简体   繁体   English

Tkinter小部件放置不正确

[英]Tkinter widgets placed incorrectly

I would like to create a frame with 我想创建一个框架

  • combobox 组合框
  • then two labels 然后两个标签
  • and then another combobox. 然后是另一个组合框。

Instead I get a frame with 相反,我得到一个框架

  • two comboboxes 两个组合框
  • and then two labels 然后是两个标签

Tkinter widgets seem to be grouped by the widget type. Tkinter小部件似乎按小部件类型分组。 Please advise how to place the widgets correctly. 请告知如何正确放置小部件。 Thanks! 谢谢!

I am using Python 3.4 on Win 7 64 bit and Tcl/Tk version 8.6. 我在Win 7 64位和Tcl / Tk版本8.6上使用Python 3.4。

import tkinter as tk
from tkinter import ttk

class App(tk.Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.grid()

        self.combo1=ttk.Combobox(self)
        self.combo1["values"]=["1","2"]
        self.combo1.grid(row=1)

        self.lbl1=ttk.Label(text="AAA")
        self.lbl1.grid(row=2)

        self.lbl3=ttk.Label(text="BBB")
        self.lbl3.grid(row=3)

        self.combo2=ttk.Combobox(self)
        self.combo2["values"]=["3","4"]
        self.combo2.grid(row=4)

root=tk.Tk()
x=App()

布局不正确

This happened because you didn't set the parent of your Labels to self (the frame), try changing your labels to this: 发生这种情况是因为您没有将Labels的父级设置为self (框架),请尝试将标签更改为:

self.lbl1=ttk.Label(self, text="AAA")
...

self.lbl3=ttk.Label(self, text="BBB")

Previously they had used the default parent, which is root , so they appeared below your frame. 以前,他们使用默认的父级,即root ,因此它们显示在您的框架下方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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