0

I have started to write a Program in which I used a combobox for the first time and now I want to allign it to the grid. However, my problem is that it always ends up at the very bottom of all my widgets (Picture: http://i.imgur.com/uTzyOFB.png?1). In addition, here is my code:

from tkinter import *
from tkinter import ttk

class Application(ttk.Frame):
    """A application"""
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

def create_widgets(self):
    """this creates all the objects in the window"""

    self.title_lbl = ttk.Label(self,text = "Title"
                               ).grid(column = 0, row = 0,sticky = W)
    self.label = ttk.Label(self, text = "label"
                                           ).grid(column = 0,
                                                  row = 1,sticky = W)
    self.combovar = StringVar()
    self.combo = ttk.Combobox(self.master,textvariable = self.combovar,
                                              state = 'readonly')
    self.combo['values'] = ('A', 'B',
                                            'C', 'D',
                                            'E', 'F',
                                            'G', 'H', 'I')
    self.combo.current(0)
    self.combo.grid(column = 0, row = 2, sticky = W)


    self.button1 = ttk.Button(self, text = "Button1"
                              ).grid(column = 0, row = 3,sticky = W)


    self.button2 = ttk.Button(self, text = "Button 2"
                                  ).grid(column = 0, row = 4, sticky = W)




def main():
    """Loops the window"""
    root = Tk()
    root.title("Window")
    root.geometry("500x350")
    app = Application(root)
    root.mainloop()

main()

As you can hopefully see, in my code the Combobx is supposed to be at row 2, right below the Label. Instead it is under the 2 buttons which are in rows 3 and 4. I have no clue why it is happening.

I am new to python 3.3 but I am fairly familiar with 2.7. I am also new to stack overflow so please bear with me. Any help is welcome.

4

1 に答える 1

1

Combobox ウィジェットに別の親を使用しているため、フレームの 3 行目ではなく、ルート要素の 2 番目の行に配置されます (したがって、フレームのラベルとボタンの下):

self.label = ttk.Label(self, ...)
# ...
self.combo = ttk.Combobox(self.master, ...)

selfそれを解決するために親として設定するだけです:

self.combo = ttk.Combobox(self, ...)
于 2013-06-05T21:01:57.727 に答える