所以我创建了一个程序,然后单击第一个单选按钮,然后单击显示“获取总成本”的按钮,当窗口打开时,它并没有给我正确的价值。
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个轮胎获得两个轮胎等的值。
答案 0 :(得分:1)
有很多不必要的代码块。我已对其进行了更正,但是它可以变得更好。
您不需要两个mainloop
,一个就足够显示所需的Toplevel
或Tk
窗口。所以我删除了window17.mainloop()
。
还使用Toplevel
窗口代替另一个主窗口Tk
。 Toplevel exists.
有很多方法可以获取所需的值,我只是使用了最简单的方法来删除amgtires()
,因为没有它就可以完成。
我使用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()
您可以使它变得更好,希望对您有所帮助。