将Python代码转换为使用按钮

2024-09-25 00:36:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经试过了,但是我不知道如何使用按钮而不是画布来实现这个代码。这是一个使用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()

Tags: andtexteventifcreatefillcanvasblack
1条回答
网友
1楼 · 发布于 2024-09-25 00:36:26

你没有说为什么你在使用按钮上有问题,所以我不知道你想解决什么问题。当然,可以创建一个按钮的网格,当然按钮在点击时调用函数也是可能的。你知道吗

对我来说,这里真正的挑战是编写紧凑、易懂和易于维护的代码。您希望添加新的函数和/或新的操作符更容易,而不必修改整个GUI。你知道吗

我个人会使用面向对象的方法,创建表示数字、函数和运算符的自定义对象。因为您使用的是非OO方法,所以我建议您创建一些helper函数来抽象出一些细节。你知道吗

有三种类型的按钮:数字、运算符(“+”、“-”等)和函数(“C”、“=”)。我将为每种类型创建一对函数:一个函数用于创建按钮,另一个函数用于响应按钮。这样做可以避免使用单一的单片函数来处理所有按键。你知道吗

我还将添加一个helper函数来布局按钮,以便更容易在代码中可视化最终产品。你知道吗

让我们从数字按钮开始。我们知道数字按钮所要做的就是把数字插入计算器。那么,让我们首先编写一个函数来实现这一点。在本例中,我假设您有一个entry小部件来保存该值,因为处理canvas文本对象很麻烦:

def do_number(n):
    global entry # the entry where we should insert the number
    entry.insert("end", n)

如果我们用do_number("3")调用这个函数,它将在计算器中插入“3”。我们可以对所有的按钮使用相同的函数,我们所要做的就是输入要插入的数字。您可以创建名为do_operatordo_function的类似函数,它们接受按钮的标签并执行适当的操作。如果你想的话,你也可以为每个按钮提供一个独特的功能。你知道吗

要创建按钮,我们需要一个小助手函数,可以将按钮绑定到该函数。本例使用lambda,但可以使用functools.partial,也可以使用函数工厂。lambda需要最少的额外步骤,所以我们继续。你知道吗

def number(number):
    global frame
    b = Button(frame, text=label, command=lambda: do_number(number))
    return b

当我们像number("4")那样调用这个函数时,它将创建一个以“4”作为标签的按钮。它还将以数字“4”作为参数调用函数do_number,以便插入正确的字符串。然后,您可以创建名为“operator”和“function”的类似函数来创建用作运算符和函数的按钮。你知道吗

Note 1: I'm generally against using global variables in this manner. Generally speaking it better to pass in the containing frame rather than rely on a global. In this specific case the use of the global makes the code a bit more compact, as you'll see in another paragraph or two. If we were building this calculator as class, we could use an instance variable instead of a global.

Note 2: declaring the variable as global here has no real effect. Globals must only be declared when you want to modify the variable. However, I put the global statement in to serve as a declaration that I intend for the variable named frame to be global. This is purely a matter of personal preference

所以,现在我们可以创建每次的按钮,让它们调用一个具有唯一参数的函数。现在只剩下使用grid来组织按钮了。另一个助手函数将使这更容易:

def make_row(row, *buttons):
    for column,button in enumerate(buttons):
        button.grid(row=row, column=column, sticky="nsew")

这个helper函数让我们传入一个按钮列表,并将它们排成一行。现在,结合我们的助手函数来创建按钮,我们可以用如下方式创建GUI:

frame = Frame(window, bg="beige")
entry = Entry(frame, borderwidth=1, relief="solid")
entry.grid(row=0, column=0, columnspan=4, sticky="nsew")
make_row(1, number("7"),   number("8"),   number("9"), operator("/"))
make_row(2, number("4"),   number("5"),   number("6"), operator("*"))
make_row(3, number("1"),   number("2"),   number("3"), operator("-"))
make_row(4, number("0"), function("="), function("C"), operator("+"))

注意现在可以在代码中看到按钮的布局。如果您想添加另一列,添加另一行,或者重新排列现有的按钮,那么这样做应该是不言而喻的。你知道吗

当然,这不是唯一的办法。您可以在不使用助手函数的情况下完成所有这些操作,只需创建几百行代码。通过花一点额外的时间将代码分解成逻辑块,您可以创建更易于阅读和维护的代码。你知道吗

相关问题 更多 >