Pergunta

I'm having some difficulty with Python and ttk. I built a UI that works correctly, but looks a bit cluttered. I wanted to add a frame so I could add some padding and enable resizing and so on, but now none of the widgets are displayed. My code is below.

Previously I was just passing parent as the parent to the widgets, which did work. I've been working through a few tutorials, and I can't see anything obviously wrong, though I'm sure it's something simple.

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()
Foi útil?

Solução

You just need to pack or grid your content frame into its parent.

Also, it helps if the code you post can be run by others without having to figure out what else to add. Here's the code I ended up with:

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()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top