简体   繁体   English

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

[英]Tkinter in Python - Entry widget not displaying

I am trying to make an entry widget display beneath a Label using the .grid() function; 我正在尝试使用.grid()函数在Label下显示条目窗口小部件; however, it is simply not showing up. 但是,它根本没有出现。 Here is my code: 这是我的代码:

#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();

The reason is because: 原因是:

  1. you don't specify a row or column for app , so it defaults to 0,0 您没有为app指定行或列,因此默认为0,0
  2. you don't specify a parent for self.previous_trans so it defaults to the root window -- the same as for the application frame 您没有为self.previous_trans指定父self.previous_trans因此它默认为根窗口-与应用程序框架相同
  3. you explicitly put self.previous_trans in row zero, column zero, which overwrites the label 您将self.previous_trans明确地放在第0行,第0列中,这将覆盖标签

You need to be giving an explicit parent of self to all of the widgets inside of Application : 您需要为Application内的所有小部件提供一个明确的self父对象:

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

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

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