如何在 guizero 对象(如按钮)上使用 Tkinter 中的 .place() 方法
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 Buttons Widget 作为其父级放入 Box Widget 中,您可以通过此处的 guizero 布局指南获取更多详细信息:
https://lawsie.github.io/guizero/layout/#boxes
尤其是对齐和位置。
根据我的尝试,如果您进行此调整,您的代码可以给出与标准 tk 相同的结果:
根据您的操作系统,布局将如下所示:
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:
The Layout will be like this based on your OS:
我遇到了同样的问题,我尝试了@Loay 解决方案,该解决方案有效。
然后我尝试变得越来越简约,看看到什么时候它不再起作用了。
最后,我能找到对我有用的最简单的解决方案是:
请注意,与您的代码的唯一区别是我首先实例化了所有小部件,然后调用
.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:
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.