简体   繁体   English

无法使用tkinter创建Entry窗口小部件

[英]Cannot create Entry widget with tkinter

I'm having some trouble creating an entry widget with tkinter. 我在使用tkinter创建条目小部件时遇到了一些麻烦。 I've imported the necessary modules and have already created several buttons and check boxes. 我已经导入了必要的模块,并且已经创建了几个按钮和复选框。 However I cannot figure out how to properly initialize the Entry. 但是我不知道如何正确初始化条目。 Here is my relevant code: 这是我的相关代码:

# Necessary Modules.------------------------------------------------------------
import win32com.client as win32
import re
from tkinter import *
from tkinter.filedialog import askopenfilename
import tkinter.messagebox


# Class for selecting the file.-------------------------------------------------
class FilenameClass():
    def __init__(self):
        self.location = 'User Import.txt'

    def getFile(self, identity):
        self.file_opt = options = {}
        options['defaultextension'] = '.txt'
        options['filetypes'] = [('Text Document (.txt)', '.txt'),
                                ('all files', '.*')]
        self.filename = askopenfilename(**self.file_opt)
        if self.filename:
            if 'User Import' in identity:
                self.location = self.filename
                app.get_txt_File['bg'] = '#0d0'
                user_file = open(self.filename, 'r')
                user_total = user_file.read()
                remove_lines = user_total.splitlines()
                for user in remove_lines:
                    regex_tab = re.compile('\\t')
                    user_info = regex_tab.split(user)
                    app.users.append(user_info)
            else:
                app.loadButton['bg'] = '#e10'


# Main Class.-------------------------------------------------------------------
class Application(Frame, Tk):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.users = []
        self.fileOBJtxt = FilenameClass()
        self.createWidgets()

    def createWidgets(self):

        # Define the default values for the options for the buttons
        # Grid layout options
        self.rowconfigure(0, minsize=5)
        self.width = 54
        self.grid(padx=5)
        self.loadButton_gopt = {'row':1,'column':1,'padx': 2, 'pady': 5}
        self.loadButton_wopt = {'width': round(self.width),'bg':'#e10'}
        self.loadButton()
        self.trainingCheckBox()
        self.signatureInput()

    def loadButton(self):
        '''Button that calls the filename class which allows the user to select
        the text file they wish to use.'''

        self.get_txt_File = Button(self, text="Load User List", \
        command=lambda: self.fileOBJtxt.getFile('User Import'))
        for key, value in self.loadButton_wopt.items():
            self.get_txt_File[key] = value
        self.get_txt_File.grid(**self.loadButton_gopt)

    def trainingCheckBox(self):

        self.training_var = IntVar()
        self.training = Checkbutton(text="Include training video?", \
        variable=self.training_var).grid(row=2, sticky=W)

    def signatureInput(self):

        Label(text="Signature Name").grid(row=4, sticky=W)
        entry = Entry(bg='#fff', width=50)
        entry.grid(row=4, column=1, columnspan=4)     

# Initialization parameters.----------------------------------------------------
if __name__ == '__main__':
    app = Application()
    app.master.title('User Notification Tool')
    app.master.geometry('405x550+100+100')
    app.master.resizable(width=False, height=False)
    app.mainloop()

I'm not seeing any tracebacks, but I can't seem to get my Entry box to show up. 我没有看到任何回溯,但是似乎无法显示“输入”框。 What am I doing wrong? 我究竟做错了什么?

EDIT: added entire code. 编辑:添加了完整的代码。

The problem with your entry field is you have not told it what frame/window to be placed in. 输入字段的问题是您尚未告知要放置在哪个框架/窗口中。

Change: 更改:

entry = Entry(bg='#fff', width=50)

To: 至:

entry = Entry(self, bg='#fff', width=50)

Make sure you always provide the window/frame that a widget is going to be placed in as the first argument. 确保始终将要放置小部件的窗口/框架作为第一个参数。 In this case it is self as self refers to a frame. 在这种情况下,它是self因为自我是指框架。

Keep in mind that your program will not be able to get() the string inside of your entry field because you have not defined it as a class attribute. 请记住,由于您尚未将程序定义为类属性,因此程序将无法get()输入字段内的字符串。 So most likely you will need to change 所以最有可能你需要改变

This: 这个:

entry = Entry(bg='#fff', width=50)
entry.grid(row=4, column=1, columnspan=4)    

To This: 为此:

self.entry = Entry(self, bg='#fff', width=50)
self.entry.grid(row=4, column=1, columnspan=4)     

This change will be necessary in order for the rest of your application to be able to read or write to the entry widget. 为了使您的应用程序的其余部分能够读取或写入条目小部件,必须进行此更改。

Change 更改

entry = Entry(bg='#fff', width=50)

to

entry = tk.Entry(bg='#fff', width=50)

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

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