框架中的ttk小部件未显示

时间:2013-09-30 09:52:15

标签: python ttk

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

1 个答案:

答案 0 :(得分:0)

您只需将内容框架打包或网格化为其父级。

此外,如果您发布的代码可以由其他人运行而不必弄清楚要添加的内容,则会有所帮助。这是我最终得到的代码:

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()