【问题标题】:Global variable is not defined when using tkinter and Combobox (GUI)使用 tkinter 和 Combobox (GUI) 时未定义全局变量
【发布时间】:2021-11-09 08:10:24
【问题描述】:

下面的代码让我在Combobox中选择三个东西,然后根据我选择的内容,函数会做一些基本的计算。

import openpyxl
from tkinter import *
import tkinter.ttk as ttk
from openpyxl import Workbook  # or "from openpyxl import Workbook": create workbook
from openpyxl import load_workbook # open/excess excel file or spreadsheet existed

def selected1(event):
    global var1 
    if mycombobox.get() == 'clam shell':
        var1 = 1
        return var1
    if mycombobox.get() == '360\u00b0':
        var1 = 2
        return var1

def selected2(event):
    global var2
    if mycombobox1.get() == 'hinge':
        var2 = 3
        return var2
    if mycombobox1.get() == 'top':
        var2 = 4
        return var2

def selected3(event):
    global var3 
    if mycombobox2.get() == 'hinge':
        var3 = 5
        return var3
    if mycombobox2.get() == 'top':
        var3 = 6
        return var3
    
def sol_analysis(selected1,selected2,selected3):
    var4 = var1 + var2 + var3
    return var4

# ========================= main ==============================

root = Tk()
root.title('Industry Solution')
root.geometry("500x800")

#  === Type ===

label_a = Label(root, text="1. type of computer")
label_a.pack(side = TOP, pady=1) 
type_com = ['Type', 'clam shell', '360\u00b0']
mycombobox = ttk.Combobox(root, values = type_com)
mycombobox.current(0)
mycombobox.bind('<<ComboboxSelected>>',selected1)
mycombobox.pack(side = TOP, pady=1) 

#  === WLAN1 position ===

label_b = Label(root, text="2. WLAN1 antenna position")
label_b.pack(side = TOP, pady=1) 
WLAN1_ant = ['WLAN1 ant. position', 'hinge', 'top']
mycombobox1 = ttk.Combobox(root, values = WLAN1_ant)
mycombobox1.current(0)
mycombobox1.bind('<<ComboboxSelected>>',selected2)
mycombobox1.pack(side = TOP, pady=1) 

#  === WLAN2 position ===

label_c = Label(root, text="3. WLAN2 antenna position")
label_c.pack(side = TOP, pady=1) 
WLAN2_ant = ['WLAN2 ant. position', 'hinge', 'top']
mycombobox2 = ttk.Combobox(root, values = WLAN2_ant)
mycombobox2.current(0)
mycombobox2.bind('<<ComboboxSelected>>',selected3)
mycombobox2.pack(side = TOP, pady=1) 

# === Result ===

sol_analysis(selected1,selected2,selected3)


root.mainloop()

在我的代码中,我想在进行计算之前选择三件事。但是,当我运行代码时,在选择任何内容之前都会弹出错误。

我得到的错误是:

NameError: name 'var1' is not defined  

我用global vari定义了所有三个变量。

【问题讨论】:

    标签: python tkinter combobox global-variables


    【解决方案1】:

    global var1 不“声明”变量;它的作用更像是“当您引用名为 var1 的变量时,请查看全局命名空间”。没有这样的变量,因此错误。

    Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
    [GCC 9.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def get_var1():
    ...     global var1
    ...     return var1
    ... 
    >>> get_var1()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in get_var1
    NameError: name 'var1' is not defined
    >>> var1 = 10
    >>> get_var1()
    10
    

    在调用sol_analysis之前尝试初始化var1等,设置为0,不会出现名称错误。

    我认为这段代码不会做你想做的事:你需要从某个东西触发sol_analysis,也许是你的GUI中的一个按钮。

    【讨论】:

      猜你喜欢
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多