因此,我创建了一个程序,我点击第一个单选按钮,然后点击按钮,说得到总成本,当窗口打开时,它没有给我正确的价值回来
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个轮胎得到两个轮胎的值等等
有很多不必要的代码块。我已经纠正了一些,但它可以得到更好的
您不需要两个
mainloop
,一个就足以显示您想要的Toplevel
或Tk
窗口。所以我去掉了window17.mainloop()
也可以使用
Toplevel
窗口而不是另一个主窗口Tk
。有一个原因Toplevel exists.
有很多方法可以获得您需要的值,我只是使用了最简单的方法,其中我删除了
amgtires()
,因为它可以不用它来完成我使用
for
循环来创建Radiobutton
使工作变得更加简单这是完整的代码。
你可以做得更好,希望这有帮助
相关问题 更多 >
编程相关推荐