框架中的ttk小部件未显示

2024-09-30 04:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用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()

Tags: selfapplication部件defbuttonrootcontentparent
1条回答
网友
1楼 · 发布于 2024-09-30 04:26:54

您只需将内容框架打包或网格化到其父框架中即可。在

另外,如果您发布的代码可以由其他人运行而不必确定要添加什么,这会有所帮助。我最后得到的代码是:

import Tkinter as tk
import ttk

#from PIL import Image

class Application:

    def __init__(self, parent):
        self.parent = parent

        self.content = ttk.Frame(parent, padding=(3,3,12,12))
        self.content.pack()
        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")

    def disableCmd(self):
        self.disButton.state(['disabled'])
        self.enButton.state(['!disabled'])


if __name__ == '__main__':
    root = tk.Tk()
    root.title("PC Control Interface")
    #img = Image("photo", file="T.ico") # "appicon.gif"
    #root.tk.call('wm','iconphoto',root._w,img)

    app = Application(root)

    root.mainloop()

相关问题 更多 >

    热门问题