我已经试过了,但是我不知道如何使用按钮而不是画布来实现这个代码。这是一个使用tkinter的计算器。我需要使这项工作使用按钮,但我试过的一切都失败了。如果有人能告诉我怎么做,甚至是怎么做,我将不胜感激。我对这种语言不熟悉,它使我困惑。谢谢
from Tkinter import *
def quit():
window.destroy()
def buttonclick(event):
global calcvalue
global savedvalue
global operator
pressed = ""
if event.x >10 and event.x <70 and event.y > 50 and event.y < 110 : pressed = 7
if event.x >10 and event.x <70 and event.y > 120 and event.y < 180 : pressed = 4
if event.x >10 and event.x <70 and event.y > 190 and event.y < 250 : pressed = 1
if event.x >10 and event.x <70 and event.y > 260 and event.y < 320 : pressed = 0
if event.x >80 and event.x <140 and event.y > 50 and event.y < 110 : pressed = 8
if event.x >80 and event.x <140 and event.y > 120 and event.y < 180 : pressed = 5
if event.x >80 and event.x <140 and event.y > 190 and event.y < 250 : pressed = 2
if event.x >150 and event.x <210 and event.y > 50 and event.y < 110 : pressed = 9
if event.x >150 and event.x <210 and event.y > 120 and event.y < 180 : pressed = 6
if event.x >150 and event.x <210 and event.y > 190 and event.y < 250 : pressed = 3
if event.x >80 and event.x <140 and event.y > 260 and event.y < 320 : pressed = "equals"
if event.x >150 and event.x <210 and event.y > 260 and event.y < 320 : pressed = "clear"
if event.x >220 and event.x <280 and event.y > 50 and event.y < 110 : pressed = "divide"
if event.x >220 and event.x <280 and event.y > 120 and event.y < 180 : pressed = "times"
if event.x >220 and event.x <280 and event.y > 190 and event.y < 250 : pressed = "minus"
if event.x >220 and event.x <280 and event.y > 260 and event.y < 320 : pressed = "plus"
if pressed == 0 or pressed == 1 or pressed == 2 or pressed == 3 or pressed == 4 or pressed == 5 or pressed == 6 or pressed == 7 or pressed == 8 or pressed == 9 :
calcvalue = calcvalue * 10 + pressed
if pressed == "divide" or pressed == "times" or pressed == "minus" or pressed == "plus" :
operator = pressed
savedvalue = calcvalue
calcvalue = 0
if pressed == "equals":
if operator == "divide": calcvalue = savedvalue /calcvalue
if operator == "times": calcvalue = savedvalue * calcvalue
if operator == "minus": calcvalue = savedvalue - calcvalue
if operator == "plus": calcvalue = savedvalue + calcvalue
if pressed == "clear":
calcvalue = 0
displayupdate()
canvas.update()
def displayupdate():
canvas.create_rectangle(10, 10, 280, 40, fill="white", outline="black")
canvas.create_text(260, 25, text=calcvalue,font="Times 20 bold",anchor=E)
def main():
global window
global tkinter
global canvas
window = Tk()
window.title("Simple Calculator")
Button(window, text="Quit", width=5, command=quit).pack()
canvas = Canvas(window, width= 290, height=330, bg = 'beige')
canvas.bind("<Button-1>", buttonclick)
#Add the numbers
canvas.create_rectangle(10, 50, 50, 110, fill="yellow", outline="black")
canvas.create_text(40, 80, text="7",font="Times 30 bold")
canvas.create_rectangle(10, 120, 70, 180, fill="yellow", outline="black")
canvas.create_text(40, 150, text="4",font="Times 30 bold")
canvas.create_rectangle(10, 190, 70, 250, fill="yellow", outline="black")
canvas.create_text(40, 220, text="1",font="Times 30 bold")
canvas.create_rectangle(10, 260, 70, 320, fill="yellow", outline="black")
canvas.create_text(40, 290, text="0",font="Times 30 bold")
canvas.create_rectangle(80, 50, 140, 110, fill="yellow", outline="black")
canvas.create_text(110, 80, text="8",font="Times 30 bold")
canvas.create_rectangle(80, 120, 140, 180, fill="yellow", outline="black")
canvas.create_text(110, 150, text="5",font="Times 30 bold")
canvas.create_rectangle(80, 190, 140, 250, fill="yellow", outline="black")
canvas.create_text(110, 220, text="2",font="Times 30 bold")
canvas.create_rectangle(150, 50, 210, 110, fill="yellow", outline="black")
canvas.create_text(180, 80, text="9",font="Times 30 bold")
canvas.create_rectangle(150, 120, 210, 180, fill="yellow", outline="black")
canvas.create_text(180, 150, text="6",font="Times 30 bold")
canvas.create_rectangle(150, 190, 210, 250, fill="yellow", outline="black")
canvas.create_text(180, 220, text="3",font="Times 30 bold")
#Add the operators
canvas.create_rectangle(80, 260, 140, 320, fill="green", outline="black")
canvas.create_text(110, 290, text="=",font="Times 30 bold")
canvas.create_rectangle(150, 260, 210, 320, fill="green", outline="black")
canvas.create_text(180, 290, text="C",font="Times 30 bold")
canvas.create_rectangle(220, 50, 280, 110, fill="pink", outline="black")
canvas.create_text(250, 80, text="/",font="Times 30 bold")
canvas.create_rectangle(220, 120, 280, 180, fill="pink", outline="black")
canvas.create_text(250, 150, text="*",font="Times 30 bold")
canvas.create_rectangle(220, 190, 280, 250, fill="pink", outline="black")
canvas.create_text(250, 220, text="-",font="Times 30 bold")
canvas.create_rectangle(220, 260, 280, 320, fill="pink", outline="black")
canvas.create_text(250, 290, text="+",font="Times 30 bold")
#Setup the display
canvas.create_rectangle(10, 10, 280, 40, fill="white", outline="black")
global calcvalue
calcvalue = 0
displayupdate()
canvas.pack()
window.mainloop()
main()
你没有说为什么你在使用按钮上有问题,所以我不知道你想解决什么问题。当然,可以创建一个按钮的网格,当然按钮在点击时调用函数也是可能的。你知道吗
对我来说,这里真正的挑战是编写紧凑、易懂和易于维护的代码。您希望添加新的函数和/或新的操作符更容易,而不必修改整个GUI。你知道吗
我个人会使用面向对象的方法,创建表示数字、函数和运算符的自定义对象。因为您使用的是非OO方法,所以我建议您创建一些helper函数来抽象出一些细节。你知道吗
有三种类型的按钮:数字、运算符(“+”、“-”等)和函数(“C”、“=”)。我将为每种类型创建一对函数:一个函数用于创建按钮,另一个函数用于响应按钮。这样做可以避免使用单一的单片函数来处理所有按键。你知道吗
我还将添加一个helper函数来布局按钮,以便更容易在代码中可视化最终产品。你知道吗
让我们从数字按钮开始。我们知道数字按钮所要做的就是把数字插入计算器。那么,让我们首先编写一个函数来实现这一点。在本例中,我假设您有一个entry小部件来保存该值,因为处理canvas文本对象很麻烦:
如果我们用
do_number("3")
调用这个函数,它将在计算器中插入“3”。我们可以对所有的按钮使用相同的函数,我们所要做的就是输入要插入的数字。您可以创建名为do_operator
和do_function
的类似函数,它们接受按钮的标签并执行适当的操作。如果你想的话,你也可以为每个按钮提供一个独特的功能。你知道吗要创建按钮,我们需要一个小助手函数,可以将按钮绑定到该函数。本例使用
lambda
,但可以使用functools.partial
,也可以使用函数工厂。lambda
需要最少的额外步骤,所以我们继续。你知道吗当我们像
number("4")
那样调用这个函数时,它将创建一个以“4”作为标签的按钮。它还将以数字“4”作为参数调用函数do_number
,以便插入正确的字符串。然后,您可以创建名为“operator”和“function”的类似函数来创建用作运算符和函数的按钮。你知道吗所以,现在我们可以创建每次的按钮,让它们调用一个具有唯一参数的函数。现在只剩下使用
grid
来组织按钮了。另一个助手函数将使这更容易:这个helper函数让我们传入一个按钮列表,并将它们排成一行。现在,结合我们的助手函数来创建按钮,我们可以用如下方式创建GUI:
注意现在可以在代码中看到按钮的布局。如果您想添加另一列,添加另一行,或者重新排列现有的按钮,那么这样做应该是不言而喻的。你知道吗
当然,这不是唯一的办法。您可以在不使用助手函数的情况下完成所有这些操作,只需创建几百行代码。通过花一点额外的时间将代码分解成逻辑块,您可以创建更易于阅读和维护的代码。你知道吗
相关问题 更多 >
编程相关推荐