Как напечатать цифры в ткинтере на новой странице - PullRequest
0 голосов
/ 27 апреля 2019

Итак, я создал программу, и я нажимаю первую радиокнопку и нажимаю кнопку с надписью «Получить полную стоимость», и когда открывается окно, оно не возвращает мне правильное значение.

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 получили значение для двух шин и т. Д.

1 Ответ

1 голос
/ 27 апреля 2019

Там много ненужного блока кода.Я немного исправил это, оно может поправиться, хотя.

  1. Вам не нужно два mainloop, одного достаточно, чтобы показать столько Toplevel или Tk окна, которые вы хотите.Поэтому я удалил 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()

Вы можете сделать это намного лучше, надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...