如何在新页上打印数字

2024-06-25 23:50:09 发布

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

因此,我创建了一个程序,我点击第一个单选按钮,然后点击按钮,说得到总成本,当窗口打开时,它没有给我正确的价值回来

import tkinter as tk
#make a new window
window10 = tk.Tk()
#lable what the page is
amgpage = tk.Label(window10, text="Mercedes Benz AMG Maintenance Calculator")
amgpage.pack(anchor='n')

#ask the user how many tires are needed
amgpage = tk.Label(window10, text="How many new tires do you need?")
amgpage.pack(anchor='w')

gr1=tk.IntVar()

def amgtires():
    x=227.92
    tiresa=(gr1.get())

    if (tiresa == 1):
        c=x
    elif(tiresa == 2):
        c=x+x
    elif(tiresa == 3):
        b=x+x+x
    elif(tiresa == 4):
        d=x+x+x+x
    elif(tiresa == 5):
        e=0
    return tiresa

def amgtotal():
#open a new page
    window17 = tk.Tk()
    a = amgtires()

    #total cost for tires
    amgpage = tk.Label(window17, text="Tire Cost: $"+ str(a))
    amgpage.pack(anchor='w')

    #size for window
    window17.geometry("400x400")

    #window title   
    window17.title("Mercedes Benz AMG Maintenance Total")

    #end of window 17   
    window17.mainloop()


#radio button on how many tire are needed
tire = tk.Radiobutton(window10, text="1",value=1,variable=gr1,command=amgtires).pack(anchor='w' )
tire = tk.Radiobutton(window10, text="2",value=2,variable=gr1,command=amgtires).pack(anchor='w' )
tire = tk.Radiobutton(window10, text="3",value=3,variable=gr1,command=amgtires).pack(anchor='w' )
tire = tk.Radiobutton(window10, text="4",value=4,variable=gr1,command=amgtires).pack(anchor='w' )
tire = tk.Radiobutton(window10, text="None",value=5,variable=gr1,command=amgtires).pack(anchor='w' )

#Get the total cost for maintenace
amgpage = tk.Button(window10,text= "Get total cost", command = amgtotal,fg = 'Green')
amgpage.pack(anchor='w')



#size for window size
window10.geometry("700x400")

#window title       
window10.title("Mercedes Benz AMG Maintenance Calculator")

#end of window10 loop
window10.mainloop()

所以当我点击我想要一个轮胎时,我只想要227.92和2个轮胎得到两个轮胎的值等等


Tags: textvaluewindowcommandpacktkanchorradiobutton
1条回答
网友
1楼 · 发布于 2024-06-25 23:50:09

有很多不必要的代码块。我已经纠正了一些,但它可以得到更好的

  1. 您不需要两个mainloop,一个就足以显示您想要的ToplevelTk窗口。所以我去掉了window17.mainloop()

  2. 也可以使用Toplevel窗口而不是另一个主窗口Tk。有一个原因Toplevel exists.

  3. 有很多方法可以获得您需要的值,我只是使用了最简单的方法,其中我删除了amgtires(),因为它可以不用它来完成

  4. 我使用for循环来创建Radiobutton使工作变得更加简单


这是完整的代码。

import tkinter as tk
#make a new window
window10 = tk.Tk()
#lable what the page is
amgpage = tk.Label(window10, text="Mercedes Benz AMG Maintenance Calculator")
amgpage.pack(anchor='n')

#ask the user how many tires are needed
amgpage = tk.Label(window10, text="How many new tires do you need?")
amgpage.pack(anchor='w')

gr1=tk.IntVar()
tire_cost = tk.DoubleVar()

def amgtotal():
#open a new page
    window17 = tk.Toplevel()  # Use Toplevel instead of tk.

    #total cost for tires
    amgpage = tk.Label(window17, text="Tire Cost: $"+ str(tire_cost.get()) )
    amgpage.pack(anchor='w')

    #size for window
    window17.geometry("400x400")

    #window title   
    window17.title("Mercedes Benz AMG Maintenance Total")


#radio button on how many tire are needed
for i in range(4):
    tire = tk.Radiobutton(window10, text=i, variable = gr1, value=i)
    tire['command'] = lambda i=i : tire_cost.set( 227.9*i )
    tire.pack(anchor='w')

tire = tk.Radiobutton(window10, text="None", value=5, variable=gr1)
tire.pack(anchor='w' )

#Get the total cost for maintenace
amgpage = tk.Button(window10,text= "Get total cost", command = amgtotal,fg = 'Green')
amgpage.pack(anchor='w')

#size for window size
window10.geometry("700x400")

#window title       
window10.title("Mercedes Benz AMG Maintenance Calculator")

#end of window10 loop
window10.mainloop()

你可以做得更好,希望这有帮助

相关问题 更多 >