2017-10-16 92 views
0

我在使用tkinter創建入口小部件時遇到了一些麻煩。我已經導入了必要的模塊,並且已經創建了幾個按鈕和複選框。但是我無法弄清楚如何正確地初始化Entry。這裏是我的相關代碼:無法使用tkinter創建Entry小部件

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

我沒有看到任何回溯,但我似乎無法讓我的輸入框顯示出來。我究竟做錯了什麼?

編輯:添加完整的代碼。

+1

你可以顯示你的其他代碼嗎? – mentalita

+0

當然,請參閱我編輯的其餘代碼。 – NoOrangeJuice

回答

2

與輸入字段的問題是你還沒有告訴它被放置在哪一幀/窗口

更改:

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

收件人:

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

確保您始終提供窗口/框架,即widget將作爲第一個參數放置。在這種情況下,它是self,因爲self表示一個幀。

請記住,您的程序將無法在輸入字段中輸入字符串get(),因爲您尚未將其定義爲類屬性。所以,最有可能你會需要改變

此:

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

這樣:

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

這種變化將是必要的,以便爲您的應用程序的其餘部分能讀或寫入入口小部件。

+0

乾杯這完美的工作。這是很好的知道,代替使用主人,我仍然需要提供自我。感謝您獲取入口屬性,我還沒有實現'get()',但是您爲我節省了一些時間。 – NoOrangeJuice

-1

變化

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

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

我試過這樣做,但我得到一個未定義的錯誤。當我創建我的複選框時,我只需要有'Checkbutton'。 'Entry'具體需要'tk.'嗎?感謝您的答覆。 – NoOrangeJuice

+0

person:這不正確,因爲OP沒有導入'tkinter as tk',而是導入爲*。 –

+0

當我發佈答案時,導入行不在那裏,只是類Application(Frame,Tk)。我做了一個假設 – person

相關問題