2017-02-19 2 views
0

파이썬 2.7을 사용하고 있으며 코드 실행 중 이상한 오류가 발생했습니다.파이썬 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) 

내가 그것을 완벽하게 작동하지만, 파이썬 2.7와 함께이 오류가 발생 파이썬 3를 실행하려고 할 때. 이 라인에 내가 잘못하고있는 것이 있습니까?

감사합니다.

코드 :

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에서는 기본적으로이 작업이 수행되는 것으로 보이지만, 2.7에서는 그렇지 않습니다.

코드에는 다른 이상한 요소가 많이 있습니다 (예 : 실제로 메뉴 버튼이나 메뉴를 만들지 않는 방식 등). 재귀 호출을 사용하여 여러 가지 이상한 작업을 수행 할 수 있습니다. 자신의 코드입니까? log_in_pageset_menu_button 메서드에서 직접 호출해야합니다. 우선 순위의 "놀라운"분포이므로 호출하지 마십시오. 대신 코드를 훨씬 간단하게 유지하는 데 집중하십시오.

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 

적어도 놀랄만큼 잘못 보이지는 않습니다.