python - python 2.7 Tkinter tcl错误

标签 python python-2.7 tkinter error-handling tcl

我使用的是python 2.7,尝试执行我的代码时遇到了一个奇怪的错误。

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

最佳答案

您应该使用Menubutton小部件的方式是将菜单附加到它。似乎在Python 3 Tkinter中,默认情况下会为您完成此操作,让您摆脱草率的束缚,但在2.7中却没有。

您的代码中还有许多其他奇怪的事情(例如,您似乎根本没有创建菜单按钮或菜单的方式,并且可能通过递归调用自己的代码来处理其他各种奇怪的事情?此外,您可以直接在log_in_page方法中调用set_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

至少看起来并不奇怪。

关于python - python 2.7 Tkinter tcl错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329631/

相关文章:

macos - Mac 上的 Pip 安装失败

python-2.7 - pandas groupby 分组和亚组级别分析

python - 使用Tkinter时线程停止循环

Python - 在单独的子进程或线程中运行 Autobahn|Python asyncio websocket 服务器

python - 对嵌套元组的内容进行分组

python - 使用 "wait_variable()"时无法退出 tkinter 应用程序

python - Redis PubSub 未重新连接

python - 你怎么知道github存储库是用于python 2还是python 3

python - 将节点添加到第一个文档树并保存到文件

python - 为什么对于整数而不是 float ,ndigits=None 上的 round 加注?