[英]Tkinter widgets placed incorrectly
I would like to create a frame with 我想创建一个框架
Instead I get a frame with 相反,我得到一个框架
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.