简体   繁体   中英

Can't allign combobox to the desired spot on my grid in python

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.

You are usng a different parent for the Combobox widget, so it is not placed in the third row of the frame, but the second one of the root element (and therefore, below the labels and the buttons of the frame):

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

Just set self as the parent to solve it:

self.combo = ttk.Combobox(self, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM