我在使用Python和ttk时遇到了一些困难。我构建了一个可以正常工作的UI,但是看起来有点混乱。我想添加一个框架,这样我可以添加一些填充和启用大小调整等,但现在没有显示任何窗口小部件。我的代码在下面。在
以前我只是将parent
作为父级传递给小部件,这样做确实有效。我已经完成了一些教程,我看不出有什么明显的错误,尽管我确信这是很简单的。在
class Application:
def __init__(self, parent):
self.parent = parent
self.content = ttk.Frame(parent, padding=(3,3,12,12))
self.content.columnconfigure(1, weight=1)
self.content.rowconfigure(1, weight=1)
self.row1()
def row1(self):
self.enButton = ttk.Button(self.content, text="Enable", command=self.enableCmd)
self.disButton = ttk.Button(self.content, text="Disable", command=self.disableCmd)
self.enButton.grid(column=1, row=1)
self.disButton.grid(column=2, row=1)
self.disButton.state(['disabled'])
def enableCmd(self):
self.disButton.state(['!disabled'])
self.enButton.state(['disabled'])
self.ser.write("pe001\n")
if __name__ == '__main__':
root = Tk()
root.title("PC Control Interface")
img = Image("photo", file="appicon.gif")
root.tk.call('wm','iconphoto',root._w,img)
app = Application(root)
root.mainloop()
您只需将内容框架打包或网格化到其父框架中即可。在
另外,如果您发布的代码可以由其他人运行而不必确定要添加什么,这会有所帮助。我最后得到的代码是:
相关问题 更多 >
编程相关推荐