如何在 guizero 对象(如按钮)上使用 Tkinter 中的 .place() 方法

发布于 2025-01-17 03:38:10 字数 941 浏览 1 评论 0原文

我正在尝试在 guizero 按钮 对象上使用 .place() Tkinter 方法。

使用 Tkinter,代码将如下所示:

vidbutton = tk.Button(w, text="Video", command = connectgp)
vidbutton.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)

shutterbutton = tk.Button(w, text="Shutter", command = connectgp)
shutterbutton.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

执行时,上述代码将显示两个按钮以正确的间距比例彼此相邻放置。

我正在尝试使用 guizero 来实现此操作,而不是使用以下代码:

connectbutton = PushButton(app, text="Connect Gopros", command=connect)
connectbutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)

videobutton = PushButton(app, text="Video", command=video)
videobutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

对于 guizero 代码,只有最后一个按钮将显示正确的位置和 .place对于之前的 guizero 对象, () 被完全忽略。

非常困惑,建议会有所帮助。

I am trying to use the .place() Tkinter method on a guizero pushbutton object.

With Tkinter the code would look like:

vidbutton = tk.Button(w, text="Video", command = connectgp)
vidbutton.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)

shutterbutton = tk.Button(w, text="Shutter", command = connectgp)
shutterbutton.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

When executed the above code will show two buttons placed next to each other with the correct ratio of spacing.

I am trying to implement this using guizero instead with the following code:

connectbutton = PushButton(app, text="Connect Gopros", command=connect)
connectbutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)

videobutton = PushButton(app, text="Video", command=video)
videobutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

For the guizero code only the last button made will display with the correct placement and the .place() is completely ignored for the previous guizero objects.

Very confused and advice would help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月牙弯弯 2025-01-24 03:38:10

您需要将 Buttons Widget 作为其父级放入 Box Widget 中,您可以通过此处的 guizero 布局指南获取更多详细信息:
https://lawsie.github.io/guizero/layout/#boxes
尤其是对齐和位置。

根据我的尝试,如果您进行此调整,您的代码可以给出与标准 tk 相同的结果:

from guizero import *

def connect():
    print("Connected!")


def video():
    print("Video!")

app = App(title="guizero")

botton_box = Box(app)
botton_box2 = Box(app)
botton_box.tk.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)
botton_box2.tk.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

connectbutton = PushButton(botton_box,width="fill", height="fill", align="left", text="Connect Gopros", command=connect)
videobutton = PushButton(botton_box2, width="fill", height="fill", align="right",text="Video", command=video)


app.display()

根据您的操作系统,布局将如下所示:
输入图片此处描述

You need to put the Buttons Widget inside a Box Widget as its parent, you can get more details by following up with guizero layout guidance here:
https://lawsie.github.io/guizero/layout/#boxes
especially the alignment and location.

Based on what I tried, your code can give the same result of the standard tk if you made this adjustment:

from guizero import *

def connect():
    print("Connected!")


def video():
    print("Video!")

app = App(title="guizero")

botton_box = Box(app)
botton_box2 = Box(app)
botton_box.tk.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)
botton_box2.tk.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

connectbutton = PushButton(botton_box,width="fill", height="fill", align="left", text="Connect Gopros", command=connect)
videobutton = PushButton(botton_box2, width="fill", height="fill", align="right",text="Video", command=video)


app.display()

The Layout will be like this based on your OS:
enter image description here

A君 2025-01-24 03:38:10

我遇到了同样的问题,我尝试了@Loay 解决方案,该解决方案有效。
然后我尝试变得越来越简约,看看到什么时候它不再起作用了。

最后,我能找到对我有用的最简单的解决方案是:

from guizero import *

def connect():
    print("Connected!")

def video():
    print ("Video!")


app = App(title="guizero")

# Instantiate the widgets first
vidbutton = PushButton(app, text="Video", command = video)
shutterbutton = PushButton(app, text="Shutter", command = connect)

# Call all the .place() methods afterward
vidbutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)
shutterbutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

app.display()

请注意,与您的代码的唯一区别是我首先实例化了所有小部件,然后调用 .place()方法。
顺序应该不重要,但确实如此。

I had the same problem and I tried @Loay solution which works.
Then I tried to become more and more minimalistic to see at which point it wouldn't work anymore.

In the end, the simplest solution I could find that works for me is:

from guizero import *

def connect():
    print("Connected!")

def video():
    print ("Video!")


app = App(title="guizero")

# Instantiate the widgets first
vidbutton = PushButton(app, text="Video", command = video)
shutterbutton = PushButton(app, text="Shutter", command = connect)

# Call all the .place() methods afterward
vidbutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.02, rely=0.02)
shutterbutton.tk.place(relheight=0.176, relwidth=0.176, relx=0.196, rely=0.02)

app.display()

Note that the only difference with your code is that I instantiated all the widgets first and then called the .place() methods.
The order should not matter, but it does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文