2017-02-19 111 views
0

我正在使用python 2.7,並且在嘗試執行我的代碼時遇到了一個奇怪的錯誤。python 2.7 Tkinter tcl錯誤

Traceback (most recent call last): 
    File "E:/cyber/PYTHON/client/main.py", line 16, in <module> 
    main() 
    File "E:/cyber/PYTHON/client/main.py", line 9, in main 
    menubutton_auth = auth_page.set_menu_button(root) 
    File "E:\cyber\PYTHON\client\auth_page.py", line 15, in set_menu_button 
    menubutton.menu.add_command(label=LOG_IN, command=self.log_in_page) 
    File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py", line 2683, in add_command 
    self.add('command', cnf or kw) 
    File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py", line 2674, in add 
    self._options(cnf, kw)) 
_tkinter.TclError: invalid command name ".36805008.36805608" 

錯誤是參照下面的命令:

menubutton.menu.add_command(label=LOG_IN, command=self.log_in_page) 

,當我試圖使用python 3它完美地工作,但與Python 2.7它提出了這個錯誤執行它。有什麼我做錯了這條線?

謝謝。

代碼:

from pages_menu import * 
from Tkinter import * 
import tkMessageBox 


class AuthPage(Page): 
    def __init__(self, root): 
     Page.__init__(self, root) 
     self.root = root 
     self.socket = None 

    def set_menu_button(self, root): 
     menubutton = super(AuthPage, self).set_menu_button(root) 
     self.log_in_page() 
     menubutton.menu.add_command(label=LOG_IN, command=self.log_in_page) 
     menubutton.menu.add_command(label=REGISTER, command=self.register_page) 
     return menubutton 

    def log_in_page(self): 
     self.clear_screen(self.root) 
     self.add_elements(self.root, LOG_IN) 
     text = Label(self.root, bd=0, font=self.font1, text=LOG_IN_TEXT, pady=100) 
     text.pack() 
     self.display_structure() 
     l = Label(self.root, pady=20) 
     l.pack() 
     button = Button(self.root, bg=ROYAL_BLUE, activebackground=ROYAL_BLUE, 
        font=self.font1, fg=WHITE, text=LOG_IN, 
        command=self.log_in_user) 
     button.pack() 

    def register_page(self): 
     self.clear_screen(self.root) 
     self.add_elements(self.root, REGISTER) 
     text = Label(self.root, bd=0, font=self.font1, text=REGISTER_TEXT, pady=40) 
     text.pack() 
     self.display_structure() 
     global entry_email 
     label_email = Label(self.root, fg=CHOCOLATE, bd=0, font=self.font1, text=EMAIL, pady=20) 
     label_email.pack() 
     entry_email = Entry(self.root, bg=GREEN, bd=5, font=self.font1, exportselection=0, fg=RED) 
     entry_email.pack() 
     l = Label(self.root, pady=20) 
     l.pack() 
     button = Button(self.root, bg=ROYAL_BLUE, activebackground=ROYAL_BLUE, 
        font=self.font1, fg=WHITE, text=LOG_IN, 
        command=self.register_user) 
     button.pack() 

    def display_structure(self): 
     global entry_username 
     global entry_password 
     label_username = Label(self.root, fg=CHOCOLATE, bd=0, font=self.font1, text=USERNAME, pady=20) 
     label_username.pack() 
     entry_username = Entry(self.root, bg=GREEN, bd=5, font=self.font1, exportselection=0, fg=RED) 
     entry_username.pack() 
     label_password = Label(self.root, fg=CHOCOLATE, bd=0, font=self.font1, text=PASSWORD, pady=20) 
     label_password.pack() 
     entry_password = Entry(self.root, bg=GREEN, bd=5, font=self.font1, exportselection=0, fg=RED, show="*") 
     entry_password.pack() 

    def log_in_user(self): 
     global entry_username 
     global entry_password 
     request = "database#login#" + entry_username.get() + "#" + entry_password.get() 
     self.make_socket() 
     self.socket.send(request) 
     answer = self.socket.recv(CHECK_BUFFER) 
     if answer == OK: 
      save_username(entry_username.get()) 
      self.enter() 
     else: 
      tkMessageBox.showwarning("INVALID", "Invalid username or password") 

    def register_user(self): 
     global entry_username 
     global entry_password 
     global entry_email 
     request = "database#register#" + entry_username.get() + "#" + entry_password.get() + "#" + entry_email.get() 
     self.make_socket() 
     self.socket.send(request) 
     answer = self.socket.recv(CHECK_BUFFER) 
     if answer == OK: 
      save_username(entry_username.get()) 
      self.enter() 
     else: 
      tkMessageBox.showwarning("INVALID", "Those username or password already exists") 

    def enter(self): 
     self.clear_all_screen(self.root) 
     menubutton = super(AuthPage, self).set_menu_button(self.root) 
     add_menu(self.root, menubutton) 
     home_page() 

    def make_socket(self): 
     self.socket = socket.socket() 
     self.socket.connect((SERVER, PORT)) 
+1

我想我們需要知道'Page.set_menu_button'是做什麼的。請刪除任何不相關的代碼,並製作一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve) –

回答

1

你應該使用Menubutton小部件的方法是通過菜單附加到它。看起來,在Python 3 Tkinter中,默認情況下這是爲你完成的,讓你擺脫sl,,但在2.7中它不是。

你的代碼中還有很多其他奇怪的東西(比如你似乎實際上並沒有創建menubutton或菜單的方式,並且可能會通過遞歸調用來做各種其他奇怪的事情你自己的代碼?另外,你直接用set_menu_button方法調用log_in_page方法,這是一個「令人驚訝」的優先級分配;不這樣做。)相反,堅持保持代碼更簡單,可能是這樣的:

def set_menu_button(self, root): 
     # You might want to choose another label ;-) 
     menubutton = Menubutton(root, "some label") 
     menu = Menu(menubutton) 
     menubutton.config(menu=menubutton) 
     menu.add_command(label=LOG_IN, command=self.log_in_page) 
     menu.add_command(label=REGISTER, command=self.register_page) 
     return menubutton 

這至少看起來並不出人意料的錯誤。