python - Tkinter 键绑定(bind)在消息框后不起作用

标签 python python-3.x tkinter messagebox key-bindings

我正在使用计算器,但遇到了有关按键绑定(bind)的问题。在 GUI 类启动结束时,会创建一个消息框,向用户解释绑定(bind)情况。

messagebox.showinfo("Guide", "Key bindings include: 1, 2, 3, 4, 5, 6, 7, 8, 9, 
0, ., +, -, *, /, (, ), Enter, Backspace, Insert and Delete.")

具有讽刺意味的是,这会导致所有绑定(bind)都不会响应,直到消息框关闭并且取消选择然后重新选择 tkinter 窗口。绑定(bind)的编码如下:

master.bind('<Delete>', self.delete)
master.bind('<BackSpace>', self.back)
master.bind('<Return>', self.equals)
master.bind('<Insert>', self.add_answer)

我尝试使用 focus_set() 但没有帮助。我该怎么做才能让我的键盘绑定(bind)在消息框关闭后立即响应?

这是我的完整上下文代码。

from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox  # added by @martineau

class Logic:
    def add_digit(self, *args):
        if type(args[0]) == str:
            self.expression.append(args[0])
        else:
            self.expression.append(args[0].char)
        self.labels[1].config(text="".join(self.expression))

    def add_operation(self, *args):
        if type(args[0]) == str:
            self.expression.append(args[0])
        else:
            self.expression.append(args[0].char)
        self.labels[1].config(text="".join(self.expression))

    def add_answer(self, *args):
        self.expression.extend(list(self.labels[0]['text']))
        self.labels[1].config(text="".join(self.expression))

    def delete(self, *args):
        if self.expression:
            self.expression = list()
            self.labels[1].config(text="".join(self.expression))
        else:
            self.labels[0].config(text="")

    def back(self, *args):
        self.expression = self.expression[:-1]
        self.labels[1].config(text="".join(self.expression))

    def equals(self, *args):
        equation = list()
        number = list()

        if not self.expression:
            self.labels[0].config(text='')

        for value in self.expression:
            if value in self.numpad_texts:
                number.append(value)
            else:
                if number:
                    equation.append(str(float("".join(number))))
                    number = list()
                equation.append(value)

        if number:
            try:
                equation.append(str(float("".join(number))))
            except ValueError:
                messagebox.showerror("Error", "Syntax error: Your expression has incorrect syntax")

        for i in range(len(equation)):
            if equation[i] == '(' and i != 0:
                if equation[i-1] not in self.operation_texts:
                    equation.insert(i, '*')
            elif equation[i] == ')' and i != len(equation)-1:
                if equation[i+1] not in self.operation_texts:
                    equation.insert(i+1, '*')

        if equation:
            try:
                self.labels[0].config(text=str(eval(''.join(equation))))
            except ZeroDivisionError:
                messagebox.showerror("Error", "Zero division error: Your expression has a division by zero")
            except SyntaxError:
                messagebox.showerror("Error", "Syntax error: Your expression has incorrect syntax")

class GUI(Logic):
    numpad_texts = ('7', '8', '9', '4', '5', '6', '1', '2', '3', '0', '.', 'Equals')
    operation_texts = ('/', '*', '-', '+', '(', ')')
    function_texts = ('Delete', 'Back')

    def __init__(self, master):
        master.title('Calculator')

        self.expression = list()

        self.label_frame = Frame(master)
        self.label_frame.grid(columnspan=2)

        self.labels = list()

        for i in range(2):
            self.labels.append(Label(self.label_frame))
            self.labels[i].grid(row=i, column=0, columnspan=4)

        self.labels[0].bind("<Button-1>", self.add_answer)

        self.numpad_frame = Frame(master)
        self.numpad_frame.grid(row=1, rowspan=2)

        self.numpad_buttons = list()

        for i in range(len(self.numpad_texts)):
            self.numpad_buttons.append(Button(self.numpad_frame, text=self.numpad_texts[i], command=lambda i=i: self.add_digit(self.numpad_texts[i])))
            self.numpad_buttons[i].grid(row=i//3, column=i%3)
            if self.numpad_texts != 11:
                master.bind(self.numpad_texts[i], self.add_digit)

        self.numpad_buttons[-1].config(command=self.equals)

        self.operations_frame = Frame(master)
        self.operations_frame.grid(row=1, column=1)

        self.operation_buttons = list()

        for i in range(len(self.operation_texts)):
            self.operation_buttons.append(Button(self.operations_frame, text=self.operation_texts[i], command=lambda i=i: self.add_operation(self.operation_texts[i])))
            self.operation_buttons[i].grid(row=i//2, column=i%2)
            master.bind(self.operation_texts[i], self.add_operation)

        self.functions_frame = Frame(master)
        self.functions_frame.grid(row=2, column=1)

        self.function_buttons = list()

        for i in range(len(self.function_texts)):
            self.function_buttons.append(Button(self.functions_frame, text=self.function_texts[i]))
            self.function_buttons[i].grid(row = 0, column=i%2)

        self.function_buttons[0].config(command=self.delete)
        self.function_buttons[1].config(command=self.back)

        master.bind('<Delete>', self.delete)
        master.bind('<BackSpace>', self.back)
        master.bind('<Return>', self.equals)
        master.bind('<Insert>', self.add_answer)

        messagebox.showinfo("Guide", "Key bindings include: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, ., +, -, *, /, (, ), Enter, Backspace, Insert and Delete.")

if __name__ == '__main__':
    root = Tk()
    calculator = GUI(root)
    root.mainloop()

最佳答案

Tkinters messagebox需要单独导入:

从 tkinter 导入消息框

然后在绑定(bind)消息框行下方添加以下内容:

master.focus_force()

用户关闭消息框后,这会将焦点移回到根窗口,并且所有绑定(bind)将继续工作。

关于python - Tkinter 键绑定(bind)在消息框后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45290534/

相关文章:

python - 将字典键值与多值索引进行比较

python - python3 : No module named release 上的 MySQLdb

python - 如何使用 tkinter filedialog.askopenfilename 方法避免文件选择器中的隐藏文件?

python - 无法使用正则表达式获得自定义结果

python - 类型错误 : expected string or bytes-like object while filtering the nested list of strings with RegEx

python - Tkinter 屏幕删除,具体

python - 包装标签时窗口会缩小,即使文本框更大

python - "Parseltongue": get Ruby to Speak a bit of Python?

java - 从 Minecraft 安装中提取项目、配方元数据

python - 在 PyQt 中调整窗口大小后如何继续在图像上绘图?