【发布时间】: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)
但这不起作用
【问题讨论】:
-
这将帮助您解决 AttributeError:stackoverflow.com/questions/1101750/…
标签: python user-interface tkinter