如何在新页面上打印tkinter中的数字

问题描述 投票:0回答:1

所以我创建了一个程序,然后单击第一个单选按钮,然后单击显示获取总成本的按钮,当窗口打开时,它不会给我正确的值。

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个轮胎来获得两个轮胎的价值等。

python python-3.x tkinter
1个回答
1
投票

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

  1. 你不需要两个mainloop,一个足以显示你想要的ToplevelTk窗户。所以我删除了window17.mainloop()
  2. 也可以使用Toplevel Window而不是另一个主窗口TkToplevel 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()

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

© www.soinside.com 2019 - 2024. All rights reserved.