Python中的Tkinter - 不显示条目小部件

时间:2015-10-16 23:36:37

标签: python windows tkinter widget

我正在尝试使用.grid()函数在Label下面创建一个条目窗口小部件;然而,它根本就没有出现。这是我的代码:

#demonstrates how to use a class with Tkinter

from Tkinter import *
import tkMessageBox

class Application(Frame):

    def __init__(self, master):
        """ Initializes the Frame"""
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.previous_trans = Text(width = 100, height = 5, wrap = WORD)
        self.previous_trans.grid(row = 0, column = 0, columnspan = 2)

        self.items = Text(width = 50, height = 16, wrap = WORD)
        self.items.grid(row = 1, column = 1, rowspan = 14, sticky = E)

        self.additem = Label(text = "Add Item")
        self.additem.grid(row = 1)

        self.myentry = Entry(self)
        self.myentry.grid(row = 2)

root = Tk();
root.title("Work In Progress")
app = Application(root)

root.mainloop();

1 个答案:

答案 0 :(得分:0)

原因是:

  1. 您没有为app指定行或列,因此默认为0
  2. 您没有为self.previous_trans指定父级,因此默认为根窗口 - 与应用程序框架相同
  3. 你明确地将self.previous_trans放在第0行,第0列,这会覆盖标签
  4. 您需要为self内的所有小部件提供Application的明确父级:

        self.previous_trans = Text(self, ...)
        self.items = Text(self, ...)
        self.additem = Label(self, ...)
        self.myentry = Entry(self, ...)