【问题标题】:How to link keyboard keys on your computer's keyboard to Tkinter buttons?如何将计算机键盘上的键盘键链接到 Tkinter 按钮?
【发布时间】:2021-04-18 16:53:34
【问题描述】:

我最近使用 Python 中的 Tkinter 设计了一个简单的 GUI 计算器。我已经使用按钮小部件创建了按钮,显然您使用鼠标单击按钮以输入表达式并单击等号按钮以获得答案。但是,我现在要做的是实现计算器,这样我就可以按下计算机键盘上的键来输入表达式,例如,按下计算机键盘上的等号按钮,而不必单击GUI 使用我的鼠标。

我该怎么做?

我试过'bind'函数,但它返回一个属性错误说:AttributeError:'NoneType'对象没有属性'bind'。我已经搜索了各种方法来解决这个问题,但似乎没有任何效果。

是否可以修复此错误(如果可以,如何解决)或者是否有其他方法可以解决?

这是我的代码:

# Python program to  create a simple GUI Calculator using Tkinter


# Part 1
# import from tkinter - Part 1
from tkinter import*


#Part 2
# Function to update expression in the text entry box
def buttonClick(numbers):
    global operator # global expression variable
    operator = operator + str(numbers)  # string concatenation
    text_Input.set(operator) # Use set method to update the expression

# Function to clear contents
# of the text entry box
def buttonClearDisplay():
    global operator

    operator = ""
    text_Input.set("")

def buttonEqualsInput():
    # I used a try and except statement
    # for handling the zero
    # division error

    try:
        global operator

        # eval function evaluates the 'expression'
        # and str function converts the result
        # into a string

        answer = str(eval(operator))
        text_Input.set(answer)

        # initializes the expression variable
        # to an empty string
        operator = ""

    except:
        # if an error is generated then handle
        # using the except block
        text_Input.set("error")
        operator = ""


# Part 3
# 3.1- create a GUI window - make window with name 'cal' - set it to the Tkinter GUI window Tk()
# use function properties to set various properties of the GUI
cal = Tk() # instantiate
cal.title("Calculator") # set title of GUI window
operator = ""

# StringVar() is the variable class
# Create an instance of this class
# used to display the numbers and operands
text_Input = StringVar()

# 3.2 TextDisplay - for creating the text entry box showing the numbers/expressions.
textDisplay = Entry(cal, font = ('Times New Roman', 20, 'bold'), textvariable = text_Input, bd = 30, insertwidth = 4,
                   bg = "powder blue", justify = 'right').grid(columnspan = 4)

# 3.3 - create Buttons and place at a particular
# locations inside the window.
# When user presses button, the command or
# function affiliated to that button is executed.
# Properties such as text, background colour, width & height etc can be edited.
# Use command - lambda to give buttons functionality.
# Use grid to place buttons at a particular position in the window.

#Row 1
button7 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "7",
              command = lambda:buttonClick(7)).grid(row = 1, column = 0)


button8 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "8",
              command = lambda:buttonClick(8)).grid(row = 1, column = 1)
button9 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "9",
              command = lambda:buttonClick(9)).grid(row = 1, column = 2)
Addition = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "+",
                  command = lambda:buttonClick("+")).grid(row = 1, column = 3)

#========================================================================================================================

#Row 2

button4 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "4",
              command = lambda:buttonClick(4)).grid(row = 2, column = 0)
button5 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "5",
              command = lambda:buttonClick(5)).grid(row = 2, column = 1)
button6 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "6",
              command = lambda:buttonClick(6)).grid(row = 2, column = 2)
Subtraction = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "-",
                     command = lambda:buttonClick("-")).grid(row = 2, column = 3)

#========================================================================================================================

#Row 3

button1 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "1",
              command = lambda:buttonClick(1)).grid(row = 3, column = 0)
button2 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "2",
              command = lambda:buttonClick(2)).grid(row = 3, column = 1)
button3 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "3",
              command = lambda:buttonClick(3)).grid(row = 3, column = 2)
Multiplication = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "*",
                        command = lambda:buttonClick("*")).grid(row = 3, column = 3)

#========================================================================================================================

#Row 4

button0 = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "0",
              command = lambda:buttonClick(0)).grid(row = 4, column = 0)
buttonClear = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "CE",
                  command = buttonClearDisplay).grid(row = 4, column = 1)
buttonEqual = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'),
                  text = "=", command = buttonEqualsInput).grid(row = 4, column = 2)
Division = Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "/",
                  command = lambda:buttonClick("/")).grid(row = 4, column = 3)

#========================================================================================================================

#Row 5

Decimal= Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = ".",
                  command = lambda:buttonClick(".")).grid(row = 5, column = 0)
Dummy1= Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "PY").grid(row = 5, column = 1)
Dummy2= Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "TH").grid(row = 5, column = 2)
Dummy3= Button(cal, padx = 16, pady = 16, bd = 8, fg = "black", font = ('arial', 20, 'bold'), text = "ON").grid(row = 5, column = 3)




#Start GUI
cal.mainloop()


我试过例如:

button7.bind("<Button7>", lambda:buttonClick)
 

但这不起作用

【问题讨论】:

标签: python user-interface tkinter


【解决方案1】:

不要调用 Button,只需运行与 Button 相同的功能即可。像这样:

cal.bind("7", lambda e:buttonClick(7))

【讨论】:

    【解决方案2】:

    感谢所有回复的人,我已经弄清楚了。

    所以,正如 Novel 刚刚所说,不要调用按钮,而是从 GUI 窗口本身声明它。因此,无论您设置等于 Tk() 的变量是什么,您都可以使用该变量来执行此操作。

    在我的例子中,它是cal = Tk(),所以我们使用变量'cal'

    代码如下所示:

    cal.bind("1", lambda e:buttonClick(1))
    cal.bind("2", lambda e:buttonClick(2))
    cal.bind("3", lambda e:buttonClick(3))
    cal.bind("4", lambda e:buttonClick(4))
    cal.bind("5", lambda e:buttonClick(5))
    cal.bind("6", lambda e:buttonClick(6))
    cal.bind("7", lambda e:buttonClick(7))
    cal.bind("8", lambda e:buttonClick(8))
    cal.bind("9", lambda e:buttonClick(9))
    cal.bind("0", lambda e:buttonClick(0))
    cal.bind("<+>", lambda e:buttonClick("+"))
    cal.bind("-", lambda e:buttonClick("-"))
    cal.bind("<*>", lambda e:buttonClick("*"))
    cal.bind("/", lambda e:buttonClick("/"))
    cal.bind("c", lambda e:buttonClearDisplay())
    cal.bind("=", lambda e:buttonEqualsInput())
    

    请注意,箭头括号内的任何运算符都表示按住 shift 和该按钮,因此例如键盘上的“+”需要按“shift 和 =”才能获得键盘上的加号,任何按钮不需要shift键按下不需要在箭头括号内。还要确保使用正确的语法正确分配函数,如此处所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      相关资源
      最近更新 更多