消息框后,Tkinter键绑定不起作用

时间:2017-07-24 21:40:02

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

我正在研究计算器,我遇到了关于我的键绑定的问题。在我的GUI类启动结束时,会创建一个消息框,说明与用户的绑定。

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

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

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

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

以下是我的上下文代码。

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

2 个答案:

答案 0 :(得分:2)

Tkinters消息框需要单独导入:

from tkinter import messagebox

然后在绑定消息框的行下面添加以下内容:

master.focus_force()

这会在用户关闭消息框后将焦点移回根窗口,并且所有绑定都将继续有效。

答案 1 :(得分:0)

我在这里发帖,因为我没有权利发表评论.. @R Oliver,我的条目小部件也有类似的问题。以下解决方案由Brian Oakley提供,作为此question中的注释,使用after_idle()并将消息作为函数工作。试试这个: 为信息创建一些功能

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

然后,在main中,使用after_idle

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