使用全局或任何其他方法?

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

我目前在使应用程序的某些部分正常工作时遇到问题。我想添加单选按钮和转换按钮,以便用户能够从华氏度转换为摄氏度,反之亦然。当将特定函数和变量添加到第二个窗口中时,选项似乎变灰并阻止代码正确执行。

我已经尝试逐渐添加某些定义和函数,这已经有效,但仍然很难将其变成我想要的正确格式。我一直在使用全局变量来确保我只与第二个窗口通信,但似乎仍然没有处理某些功能。

def openNewWindow():
    global NewWindow

    #Creates a new window using Toplevel
    NewWindow = Toplevel(master_window)

    #Sets the title of Toplevel widget
    NewWindow.title("Dave's weather app")

    #Sets the geometry of Toplevel
    NewWindow.geometry('350x350')

#Label widget to show in Toplevel
    Label(NewWindow, text = "Temperature Conversion", fg = 'gold', bg = 'dark blue', font = ('bold, 12')).pack()

#Configures window color
    NewWindow.configure(bg = 'dark blue')

#Temperature input block that allows the user to input a number within the program
    label = Label(NewWindow, text = "Enter Temperature:").pack()

    entry = Entry(NewWindow).pack()

    #Convert button acts as an enter key and allows the program to process the number
    convert_btn = Button(NewWindow, text = "Convert", command = convert_temperature).pack()

    #Result label outputs
    result_label = Label(NewWindow, text = "").pack()

#Defines the formula to convert Celsius to Fahrenheit
def Celsius_to_Fahrenheit(Celsius):
    return (1.8) * Celsius + 32
#Defines the formula to convert Fahrenheit to Celsius
def Fahrenheit_to_Celsius(Fahrenheit):
    return (0.55) * (Fahrenheit - 32)


  







#Main NewWindow welcome screen
master_window = Tk()
master_window.title("Welcome Screen")
master_window.configure(bg = 'dark blue')
master_window.geometry('200x200')


#Welcome label for the master window
label = Label(master_window, text = "Welcome!")
label.pack( pady = 10)

#Button to open Dave's weather app 
btn1 = Button(master_window, text = "Click to open Dave's Weather app", command = openNewWindow)
btn1.pack(expand = True)

#Welcome screen exit button
btn2 = Button(master_window, text = "Exit", command = exit)
btn2.pack(expand = True)


mainloop()
python tkinter
1个回答
0
投票

这是完整的代码。单选按钮应按代码所示进行声明。为了获取单选按钮的值,声明了一个名为 tempvar 的字符串变量。

tempvar = StringVar()
。通过使用
tempvar.get()
,我们可以获得该值。应使用
variable = tempvar
将字符串变量映射到单选按钮。同样,条目的值是通过
entry.get()
获得的。我没有声明全局变量,而是在顶层窗口中给出了函数
convert_temperature()
。使用
label.configure
显示输出。请注意:如果小部件被声明为具有变量名称的变量,则无法直接打包。只能使用
widget_name.pack()

将其打包到下一行
from tkinter import *


def openNewWindow():
      
    def convert_temperature():
        
        temp = entry.get()
        temp = int(float(temp))
        type = tempvar.get()
        if type == 'celc':
            result = (1.8) * temp + 32
        else: result = (0.55) * (temp - 32)
        result_label.configure(text = result)
    
    

    #Creates a new window using Toplevel
    NewWindow = Toplevel(master_window)

    #Sets the title of Toplevel widget
    NewWindow.title("Dave's weather app")

    #Sets the geometry of Toplevel
    NewWindow.geometry('350x350')

#Label widget to show in Toplevel
    Label(NewWindow, text = "Temperature Conversion", fg = 'gold', bg = 'dark blue', font = ('bold, 12')).pack()

#Configures window color
    NewWindow.configure(bg = 'dark blue')
    
    tempvar = StringVar()
    celc = Radiobutton(NewWindow, variable = tempvar, value = 'celc', text = 'Convert from Celsius to Fahrenheit')  
    celc.pack()
    
    fheit = Radiobutton(NewWindow, variable = tempvar, value = 'fheit', text = 'Convert from Fahrenheit to Celsius')
    fheit.pack()
    
#Please Note when widget is assigned to a variable name, declare variable separately and pack the variable in next line
    label = Label(NewWindow, text = "Enter Temperature:")
    label.pack()

    entry = Entry(NewWindow)
    entry.pack()

    #Convert button acts as an enter key and allows the program to process the number
    convert_btn = Button(NewWindow, text = "Convert", command = convert_temperature)
    convert_btn.pack()

    #Result label outputs
    result_label = Label(NewWindow, text = "")
    result_label.pack()
    
#Main NewWindow welcome screen
master_window = Tk()
master_window.title("Welcome Screen")
master_window.configure(bg = 'dark blue')
master_window.geometry('200x200')


#Welcome label for the master window
label = Label(master_window, text = "Welcome!")
label.pack( pady = 10)

#Button to open Dave's weather app 
btn1 = Button(master_window, text = "Click to open Dave's Weather app", command = openNewWindow)
btn1.pack(expand = True)

#Welcome screen exit button
btn2 = Button(master_window, text = "Exit", command = exit)
btn2.pack(expand = True)


master_window.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.