Home > front end >  Global variable is not defined when using tkinter and Combobox (GUI)
Global variable is not defined when using tkinter and Combobox (GUI)

Time:11-09

The following codes let me choose three things in Combobox and then based on what I chose, the function will do some basic calculation.

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()

In my code, I want to select three things before doing the calculation. However, when I ran the codes, the error popped out before choosing anything.

The error I got is:

NameError: name 'var1' is not defined  

I defined all three variables by global vari.

CodePudding user response:

global var1 doesn't "declare" the variable; it does something more like "when you reference the variable named var1, look in the global namespace". There is no such variable, hence the error.

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

Try initializing var1, etc., before you call sol_analysis, to say 0, and you won't get the name error.

I don't think this code will do what you want: you need to trigger sol_analysis from something, maybe a button in your GUI.

  • Related