Python tkinter网格管理器无法正确安装按钮
我有一个带有1个视频帧和5个按钮的应用程序。我想在上面的视频帧和按钮很好的框架下,所有在一行 这是我的密码:Python tkinter网格管理器无法正确安装按钮,python,tkinter,Python,Tkinter,我有一个带有1个视频帧和5个按钮的应用程序。我想在上面的视频帧和按钮很好的框架下,所有在一行 这是我的密码: self.root = tki.Tk() self.panel = None #create a button, that when pressed, will take the current #frame and save it to file btn= tki.Button(self.root, text="Snapshot!", comma
self.root = tki.Tk()
self.panel = None
#create a button, that when pressed, will take the current
#frame and save it to file
btn= tki.Button(self.root, text="Snapshot!", command=self.takeSnapshot)
btn.grid(sticky = tki.S)
btnturnl= tki.Button(self.root, text="Left", command = self.EinsRechts)
btnturnl.grid(sticky = tki.SW)
btnturnl2= tki.Button(self.root, text="Two Left", command = self.ZweiRechts)
btnturnl2.grid(sticky = tki.SW)
btnturnr= tki.Button(self.root, text="Right", command = self.EinsLinks)
btnturnr.grid(sticky = tki.SE)
btnturnr2= tki.Button(self.root, text="Two Right", command = self.ZweiLinks)
btnturnr2.grid(sticky = tki.SE)
self.panel = tki.Label(image=image) #in this panel the video feed gets shown
self.panel.image = image
self.panel.grid(sticky = tki.N)
这就是它的真实外观:
我对Tkinter不是很有经验,所以如果我可能错过了一个更适合我的函数,我会很乐意更改我的代码
如果您有问题,请随时提问并感谢您的帮助。您缺少实际的列和行参数:
以这个例子来实现您想要的。使用
行
和列
与excel工作表类似,因此您可以增加值,将其放置在您想要的位置
btn= tki.Button(self.root, text="Snapshot!", command=self.takeSnapshot)
btn.grid(row=1 , column=2, sticky = tki.S)
btnturnl= tki.Button(self.root, text="Left", command = self.EinsRechts)
btnturnl.grid(row=3 , column=5, sticky = tki.SW)
替换:
btn.grid(sticky = tki.S)
...
btnturnl.grid(sticky = tki.SW)
...
btnturnl2.grid(sticky = tki.SW)
...
btnturnr.grid(sticky = tki.SE)
...
btnturnr2.grid(sticky = tki.SE)
...
self.panel.grid(sticky = tki.N)
与:
如果未在网格中指定行
或列
数字
调用行
默认为最后一行
和列
默认为0
请在下面查看生成相同GUI的代码段:
与以下内容相同:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Label")
button = tk.Button(root, text="Button")
label.grid(row=0, column=0) # explicit
button.grid(row=1, column=0)
root.mainloop()
建议使用显式代码,后者是更好的代码,引用python的zen(导入此代码)
:
.grid()
需要行和列的编号,这样它就知道了您想把小部件放在哪里。谢谢您的帮助。这真的很好用。当您更改窗口大小时,是否有任何选项可以使按钮拉伸?或者使程序始终以全窗口模式启动?是的,有。搜索sticky
,grid\u row configure
,grid\u column configure
,weight
,和-全屏
。很好,是否可以拉伸视频提要并使其适合窗口大小?我有信心通过按钮来实现这一点,但视频馈送或更好地说是面板。@Xenoshell我不知道这一点。可以调整图像大小,所以如果视频是逐帧图像,我看不出为什么不可以。
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Label")
button = tk.Button(root, text="Button")
label.grid() # implicit
button.grid()
root.mainloop()
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Label")
button = tk.Button(root, text="Button")
label.grid(row=0, column=0) # explicit
button.grid(row=1, column=0)
root.mainloop()
"Explicit is better than implicit."