变量在定义并设为全局后未在子程序中定义

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

以下代码出现错误,因为它显示名称“Combo_Box Value”未定义。

from tkinter import *
from tkinter.ttk import Progressbar
from tkinter.ttk import Combobox
from tkinter.ttk import Notebook
import tkinter.font

class Pounds_Converter():
def __init__(self, parent):
    self.gui(parent)

def gui(self, parent):
    if parent == 0:
        self.w1 = Tk()
        self.w1.configure(bg = '#e4f3ff')
        self.w1.geometry('370x350')
    else:
        self.w1 = Frame(parent)
        self.w1.configure(bg = '#e4f3ff')
        self.w1.place(x = 0, y = 0, width = 370, height = 350)
    self.label3 = Label(self.w1, text = "Pounds Converter", bg = "#e4f3ff", fg = "#002d5e", font = tkinter.font.Font(family = "Myanmar Text", size = 24), cursor = "arrow", state = "normal")
    self.label3.place(x = 55, y = 50, width = 260, height = 62)
    self.label4 = Label(self.w1, text = "Pounds", bg = "#e4f3ff", fg = "#006fe8", font = tkinter.font.Font(family = "Myanmar Text", size = 12), cursor = "arrow", state = "normal")
    self.label4.place(x = 60, y = 135, width = 60, height = 22)
    self.combo1 = Combobox(self.w1, font = tkinter.font.Font(family = "Myanmar Text", size = 12), cursor = "arrow", state = "normal")
    self.combo1.place(x = 250, y = 135, width = 76, height = 22)
    self.combo1['values'] = ("Dollars", "Euros")
    self.combo1.current(0)
    self.combo1.bind("<<ComboboxSelected>>", self.Combo_Box)
    self.ltext3 = Entry(self.w1, bg = "#efffff", font = tkinter.font.Font(family = "MS Shell Dlg 2", size = 8), cursor = "arrow", state = "normal")
    self.ltext3.place(x = 20, y = 170, width = 140, height = 32)
    self.ltext3.insert(INSERT, "0.00")
    self.ltext4 = Entry(self.w1, bg = "#efffff", font = tkinter.font.Font(family = "MS Shell Dlg 2", size = 8), cursor = "arrow", state = "normal")
    self.ltext4.place(x = 220, y = 170, width = 140, height = 32)
    self.button2 = Button(self.w1, text = "Convert", bg = "#efffff", fg = "#006fe8", font = tkinter.font.Font(family = "MS Shell Dlg 2", size = 8), cursor = "arrow", state = "normal")
    self.button2.place(x = 120, y = 270, width = 130, height = 30)
    self.button2['command'] = self.Convert
    self.button3 = Button(self.w1, text = "Home", bg = "#e4f3ff", fg = "#006fe8", font = tkinter.font.Font(family = "Myanmar Text", size = 8), cursor = "arrow", state = "normal")
    self.button3.place(x = 20, y = 20, width = 40, height = 16)

def Combo_Box(self, e):
    Combo_Box_Value=self.combo1.get()
    return Combo_Box_Value


def Convert(self):
    global Combo_Box_Value
    Pounds=self.ltext3.get()
    Pounds = float(Pounds)
    if Combo_Box_Value== "Dollars":
        Dollars=Pounds*1.38
        self.ltext4.delete("0",END)
        self.ltext4.insert(INSERT,Dollars)
a = Pounds_Converter(0)
a.w1.mainloop()

这是该 GUI 小部件的完整代码。我想在组合框更改后获取它的值,然后我需要在另一个变量中使用它。

python user-interface tkinter combobox
1个回答
0
投票

以下代码出现错误,因为它显示名称“Combo_Box Value”是 未定义。

可以通过将第 49 行上的

if Combo_Box_Value
更改为
if self.combo1.get()
来解决此问题。也无需包含
global

片段:

def Convert(self):
    #global Combo_Box_Value
    Pounds=self.ltext3.get()
    Pounds = float(Pounds)
    if self.combo1.get()== "Dollars":
        Dollars=Pounds*1.38
        self.ltext4.delete("0",END)
        self.ltext4.insert(INSERT,Dollars)

截图:

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