2017-02-24 63 views
-1

我正在使用tkinter,我有2個.py文件,一個叫做「account」,另一個叫做「account_support」,我試圖在「account.py 「然後都在與該GUI的功能 」account_support.py「AttributeError:'module'對象沒有屬性'key'

這是account.py

import sys 

try: 
    from Tkinter import * 
except ImportError: 
    from tkinter import * 

try: 
    import ttk 
    py3 = 0 
except ImportError: 
    import tkinter.ttk as ttk 
    py3 = 1 

import account_support 

def vp_start_gui(): 
    '''Starting point when module is the main routine.''' 
    global val, w, root 
    root = Tk() 
    top = Add_Account (root) 
    account_support.init(root, top) 
    root.mainloop() 

w = None 
def create_Add_Account(root, *args, **kwargs): 
    '''Starting point when module is imported by another program.''' 
    global w, w_win, rt 
    rt = root 
    w = Toplevel (root) 
    top = Add_Account (w) 
    account_support.init(w, top, *args, **kwargs) 
    return (w, top) 

def destroy_Add_Account(): 
    global w 
    w.destroy() 
    w = None 


class Add_Account: 
    def __init__(self, top=None): 
     '''This class configures and populates the toplevel window. 
      top is the toplevel containing window.''' 
     _bgcolor = '#d9d9d9' # X11 color: 'gray85' 
     _fgcolor = '#000000' # X11 color: 'black' 
     _compcolor = '#d9d9d9' # X11 color: 'gray85' 
     _ana1color = '#d9d9d9' # X11 color: 'gray85' 
     _ana2color = '#d9d9d9' # X11 color: 'gray85' 
     font9 = "-family {DejaVu Sans} -size 0 -weight normal -slant " \ 
      "roman -underline 0 -overstrike 0" 

     top.geometry("400x200+407+266") 
     top.title("Add Account") 
     top.configure(highlightcolor="black") 



     self.menubar = Menu(top,font=font9,bg=_bgcolor,fg=_fgcolor) 
     top.configure(menu = self.menubar) 



     self.accName = Entry(top) 
     self.accName.place(relx=0.35, rely=0.3, relheight=0.11, relwidth=0.44) 
     self.accName.configure(background="white") 
     self.accName.configure(font="TkFixedFont") 
     self.accName.configure(selectbackground="#c4c4c4") 

     self.Label1 = Label(top) 
     self.Label1.place(relx=0.25, rely=0.1, height=38, width=176) 
     self.Label1.configure(activebackground="#f9f9f9") 
     self.Label1.configure(text='''Add Account''') 

     self.Label2 = Label(top) 
     self.Label2.place(relx=0.03, rely=0.3, height=18, width=126) 
     self.Label2.configure(activebackground="#f9f9f9") 
     self.Label2.configure(text='''Account Name:''') 

     self.key = Entry(top) 
     self.key.place(relx=0.35, rely=0.5, relheight=0.11, relwidth=0.44) 
     self.key.configure(background="white") 
     self.key.configure(font="TkFixedFont") 
     self.key.configure(selectbackground="#c4c4c4") 

     self.Label3 = Label(top) 
     self.Label3.place(relx=0.15, rely=0.5, height=21, width=76) 
     self.Label3.configure(activebackground="#f9f9f9") 
     self.Label3.configure(text='''CMA Key:''') 

     self.add = Button(top) 
     self.add.place(relx=0.53, rely=0.7, height=26, width=107) 
     self.add.configure(activebackground="#d9d9d9") 
     self.add.configure(command=account_support.addaccount) 
     self.add.configure(text='''Add Account''') 

     self.helpButton = Button(top) 
     self.helpButton.place(relx=0.8, rely=0.5, height=16, width=17) 
     self.helpButton.configure(activebackground="#d9d9d9") 
     self.helpButton.configure(command=account_support.question) 
     self.helpButton.configure(text='''?''') 






if __name__ == '__main__': 
    vp_start_gui() 

,然後account_support.py

import sys 
import webbrowser 
import tkMessageBox 

try: 
    from Tkinter import * 
except ImportError: 
    from tkinter import * 

try: 
    import ttk 
    py3 = 0 
except ImportError: 
    import tkinter.ttk as ttk 
    py3 = 1 



def addaccount(): 
    import account 
    cmakey = account.Entry.key.get() 
    acc = account.Entry.accName.get() 
    print acc 
    print cmakey 
    sys.stdout.flush() 


def question(): 
    tkMessageBox.showinfo(title="CMA Key", message="") 
    sys.stdout.flush() 

def init(top, gui, *args, **kwargs): 
    global w, top_level, root 
    w = gui 
    top_level = top 
    root = top 

def destroy_window(): 
    # Function which closes the window. 
    global top_level 
    top_level.destroy() 
    top_level = None 

if __name__ == '__main__': 
    import account 
    account.vp_start_gui() 

當我嘗試得到ac的價值count.py的鍵輸入

def addaccount(): 
    import account 
    cmakey = account.Entry.key.get() 
    acc = account.Entry.accName.get() 
    print acc 
    print cmakey 
    sys.stdout.flush() 

當你執行

cmakey = account.Entry.key.get() 
acc = account.Entry.accName.get() 

你想訪問的key屬性我得到的錯誤

File "/home/silicaandpina/VitaTools/psvimgtools-0.1-linux64/gui/psvimgtools/account_support.py", line 29, in addaccount 
    cmakey = account.Entry.key.get() 
AttributeError: class Entry has no attribute 'key' 

回答

1

您模塊中的EntryTkinteraccount 。如錯誤消息所示,Entry類沒有名爲key的屬性。

解決方案

更改addaccount功能

def addaccount(cmakey, acc): 
    print acc 
    print cmakey 
    sys.stdout.flush() 

正如你所看到的,你現在接收兩個參數都已經要使用的字符串。現在,在account中,將按鈕配置行更改爲

self.add.configure(command=lambda: account_support.addaccount(self.key.get(), self.accName.get())) 
+0

工作!謝謝! – SilicaAndPina