Hello I want some help figuring out how to change the text of a tk.label that is in a subframe
here is the code.
from time import sleep # Imported the sleep funtion from time for delays
from tkinter import * # Imported all of the tkinter functions associated with creating the GUI
from tkinter.ttk import Progressbar
from threading import Thread
import tkinter as tk
#import pyvisa
from serial import*
from tkinter import ttk
from PIL import ImageTk, Image
import threading
#####################################################################################
########################## Quick adjustments ########################################
MainTitleTextSize = 24
GeneralTitleTextSize = 20
GeneralTextSize = 18
LabelTextFont = "Geia"
ButtonTextFont = "Geia Bold"
Backgroundcolor = "Black"
Bordercolor = "cyan"
BorderThickness = 4
ProgramSelectedTextcolor = "green"
FroundFramecolor = "#8ad9ff"
FroundLabelcolor = "#8ad9ff"
FroundButtoncolor = "#8ad9ff"
Labelcolor1 = "green2"
Labelcolor2 = "#8ad9ff"
DrManhatanBlue = "#8ad9ff"
DarkMode = "#1e1e1e"
DrManhatanBlue = "#8ad9ff"
root=tk.Tk() # Instantiates a Tk object
root.title("Tester")
root.configure(bg=DarkMode)
root.geometry('1000x800')
OPCUA_Var = tk.StringVar()
#####################################################################################
########################## OPCUA Window Popup #######################################
# function to open a new window
# on a button click
def open_OPCUA_Window():
# Toplevel object which will
# be treated as a new window
OPCUAWindow = tk.Toplevel(root)
OPCUAWindow.title("OPCUA Configuration")
OPCUAWindow.configure(bg=DarkMode) # Changes the background to this color
OPCUAWindow.geometry("600x400")
# A Label widget to show in toplevel
OPCFrame = tk.LabelFrame(OPCUAWindow, text="", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
OPCLabel = tk.Label(OPCFrame, text=" OPCUA Settings " , padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
OPCcommand_label = tk.Label(OPCFrame, text =" Manual Command :", bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 12))
OPCcommand_entry = tk.Entry(OPCFrame,textvariable = OPCUA_Var, font=('calibre',12,'normal'))
OPCsub_btn = tk.Button(OPCFrame,text = 'Submit',padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 12), command = OPCsubmit)
OPCReturn = tk.Label(OPCFrame, text =" return :", bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 12))
OPCFrame.grid( row=0, column=0, sticky="w")
OPCLabel.grid( row=0, column=0, sticky="w")
OPCcommand_label.grid( row=1, column=0, sticky="w")
OPCcommand_entry.grid( row=1, column=1, sticky="E")
OPCsub_btn.grid( row=1, column=2, sticky="w")
OPCReturn.grid( row=2, column=0, sticky="E")
#####################################################################################
########### Manual input functions for OPC Configuration Window ##################
def OPCUA_COMMAND(OPCcommand):
passthrough = OPCcommand
Datapopup.config(text = passthrough)
OPCReturn.config(text = passthrough)
def OPCsubmit():
opccommand=OPCUA_Var.get()
print("The command sent was:" + opccommand)
OPCUA_COMMAND(opccommand)
OPCUA_Var.set("")
##############################################################################
###########################Navigation frame###################################
NavigationFrame = tk.LabelFrame(root, text="", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
NavigationLabel = tk.Label(NavigationFrame, text=" Navigation " , bg=DarkMode, fg=DrManhatanBlue, font=("Geia", 12))
NavigationButton3 = tk.Button(NavigationFrame, text=" OPCUA Configuration " , bg=DrManhatanBlue, fg="black", font=("Geia", 12),command = open_OPCUA_Window)
NavigationFrame.grid( row=5,padx=10,pady=0, sticky="we", columnspan=10,rowspan=2)
NavigationLabel.grid( row=0, columnspan=6, sticky="w",rowspan=2)
NavigationButton3.grid( row=1, column = 2, sticky="w",rowspan=2)
##############################################################################
###########################Data frame#########################################
DataFrame = tk.LabelFrame(root, text="", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
DataLabel = tk.Label(DataFrame, text="Data : " , bg=DarkMode, fg=DrManhatanBlue, font=("Geia", 12))
Datapopup = tk.Label(DataFrame, text=" " , bg=DarkMode, fg=DrManhatanBlue, font=("Geia", 12))
DataFrame.grid( row=3,padx=10,pady=0, sticky="we", columnspan=6,rowspan=2)
DataLabel.grid( row=0, column = 1, sticky="w",rowspan=2)
Datapopup.grid( row=0, column = 2, sticky="w",rowspan=2, columnspan=5,)
root.mainloop()
You can see that I am cable to configure the text in the "data frame" window
but when I try to change the text in the "opcua window pop up"
I get a NameError: name 'OPCReturn' is not defined
when I try to run OPCReturn.config(text = passthrough)
Hello I want some help figuring out how to change the text of a tk.label that is in a subframe
here is the code.
from time import sleep # Imported the sleep funtion from time for delays
from tkinter import * # Imported all of the tkinter functions associated with creating the GUI
from tkinter.ttk import Progressbar
from threading import Thread
import tkinter as tk
#import pyvisa
from serial import*
from tkinter import ttk
from PIL import ImageTk, Image
import threading
#####################################################################################
########################## Quick adjustments ########################################
MainTitleTextSize = 24
GeneralTitleTextSize = 20
GeneralTextSize = 18
LabelTextFont = "Geia"
ButtonTextFont = "Geia Bold"
Backgroundcolor = "Black"
Bordercolor = "cyan"
BorderThickness = 4
ProgramSelectedTextcolor = "green"
FroundFramecolor = "#8ad9ff"
FroundLabelcolor = "#8ad9ff"
FroundButtoncolor = "#8ad9ff"
Labelcolor1 = "green2"
Labelcolor2 = "#8ad9ff"
DrManhatanBlue = "#8ad9ff"
DarkMode = "#1e1e1e"
DrManhatanBlue = "#8ad9ff"
root=tk.Tk() # Instantiates a Tk object
root.title("Tester")
root.configure(bg=DarkMode)
root.geometry('1000x800')
OPCUA_Var = tk.StringVar()
#####################################################################################
########################## OPCUA Window Popup #######################################
# function to open a new window
# on a button click
def open_OPCUA_Window():
# Toplevel object which will
# be treated as a new window
OPCUAWindow = tk.Toplevel(root)
OPCUAWindow.title("OPCUA Configuration")
OPCUAWindow.configure(bg=DarkMode) # Changes the background to this color
OPCUAWindow.geometry("600x400")
# A Label widget to show in toplevel
OPCFrame = tk.LabelFrame(OPCUAWindow, text="", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
OPCLabel = tk.Label(OPCFrame, text=" OPCUA Settings " , padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
OPCcommand_label = tk.Label(OPCFrame, text =" Manual Command :", bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 12))
OPCcommand_entry = tk.Entry(OPCFrame,textvariable = OPCUA_Var, font=('calibre',12,'normal'))
OPCsub_btn = tk.Button(OPCFrame,text = 'Submit',padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 12), command = OPCsubmit)
OPCReturn = tk.Label(OPCFrame, text =" return :", bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 12))
OPCFrame.grid( row=0, column=0, sticky="w")
OPCLabel.grid( row=0, column=0, sticky="w")
OPCcommand_label.grid( row=1, column=0, sticky="w")
OPCcommand_entry.grid( row=1, column=1, sticky="E")
OPCsub_btn.grid( row=1, column=2, sticky="w")
OPCReturn.grid( row=2, column=0, sticky="E")
#####################################################################################
########### Manual input functions for OPC Configuration Window ##################
def OPCUA_COMMAND(OPCcommand):
passthrough = OPCcommand
Datapopup.config(text = passthrough)
OPCReturn.config(text = passthrough)
def OPCsubmit():
opccommand=OPCUA_Var.get()
print("The command sent was:" + opccommand)
OPCUA_COMMAND(opccommand)
OPCUA_Var.set("")
##############################################################################
###########################Navigation frame###################################
NavigationFrame = tk.LabelFrame(root, text="", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
NavigationLabel = tk.Label(NavigationFrame, text=" Navigation " , bg=DarkMode, fg=DrManhatanBlue, font=("Geia", 12))
NavigationButton3 = tk.Button(NavigationFrame, text=" OPCUA Configuration " , bg=DrManhatanBlue, fg="black", font=("Geia", 12),command = open_OPCUA_Window)
NavigationFrame.grid( row=5,padx=10,pady=0, sticky="we", columnspan=10,rowspan=2)
NavigationLabel.grid( row=0, columnspan=6, sticky="w",rowspan=2)
NavigationButton3.grid( row=1, column = 2, sticky="w",rowspan=2)
##############################################################################
###########################Data frame#########################################
DataFrame = tk.LabelFrame(root, text="", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor , font=(LabelTextFont, 18))
DataLabel = tk.Label(DataFrame, text="Data : " , bg=DarkMode, fg=DrManhatanBlue, font=("Geia", 12))
Datapopup = tk.Label(DataFrame, text=" " , bg=DarkMode, fg=DrManhatanBlue, font=("Geia", 12))
DataFrame.grid( row=3,padx=10,pady=0, sticky="we", columnspan=6,rowspan=2)
DataLabel.grid( row=0, column = 1, sticky="w",rowspan=2)
Datapopup.grid( row=0, column = 2, sticky="w",rowspan=2, columnspan=5,)
root.mainloop()
You can see that I am cable to configure the text in the "data frame" window
but when I try to change the text in the "opcua window pop up"
I get a NameError: name 'OPCReturn' is not defined
when I try to run OPCReturn.config(text = passthrough)
1 Answer
Reset to default 1The NameError: name 'OPCReturn' is not defined
occurs because the OPCReturn
label is defined inside the open_OPCUA_Window
function, making it a local variable. To fix this, you need to make OPCReturn
accessible to other functions by declaring it as a global variable.
Here's how you can fix it:
Declare
OPCReturn
as a global variable inside theopen_OPCUA_Window
function.Ensure that
OPCReturn
is referenced correctly in theOPCUA_COMMAND
function.
Here's the updated code:
def open_OPCUA_Window():
global OPCReturn # Declare OPCReturn as global
OPCUAWindow = tk.Toplevel(root)
OPCUAWindow.title("OPCUA Configuration")
OPCUAWindow.configure(bg=DarkMode)
OPCUAWindow.geometry("600x400")
OPCFrame = tk.LabelFrame(OPCUAWindow, text="", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor, font=(LabelTextFont, 18))
OPCLabel = tk.Label(OPCFrame, text=" OPCUA Settings ", padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor, font=(LabelTextFont, 18))
OPCcommand_label = tk.Label(OPCFrame, text=" Manual Command :", bg=DarkMode, fg=FroundFramecolor, font=(LabelTextFont, 12))
OPCcommand_entry = tk.Entry(OPCFrame, textvariable=OPCUA_Var, font=('calibre', 12, 'normal'))
OPCsub_btn = tk.Button(OPCFrame, text='Submit', padx=5, pady=5, bg=DarkMode, fg=FroundFramecolor, font=(LabelTextFont, 12), command=OPCsubmit)
OPCReturn = tk.Label(OPCFrame, text=" return :", bg=DarkMode, fg=FroundFramecolor, font=(LabelTextFont, 12))
OPCFrame.grid(row=0, column=0, sticky="w")
OPCLabel.grid(row=0, column=0, sticky="w")
OPCcommand_label.grid(row=1, column=0, sticky="w")
OPCcommand_entry.grid(row=1, column=1, sticky="E")
OPCsub_btn.grid(row=1, column=2, sticky="w")
OPCReturn.grid(row=2, column=0, sticky="E")
def OPCUA_COMMAND(OPCcommand):
passthrough = OPCcommand
Datapopup.config(text=passthrough)
OPCReturn.config(text=passthrough) # This will now work correctly
def OPCsubmit():
opccommand = OPCUA_Var.get()
print("The command sent was:" + opccommand)
OPCUA_COMMAND(opccommand)
OPCUA_Var.set("")
By declaring OPCReturn
as a global variable, it becomes accessible to the OPCUA_COMMAND
function, allowing you to update its text without encountering a NameError
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744400213a4572348.html
sleep
anywhere in the main thread, as your GUI will hang for as long as it'ssleep
ing. I'd also recommend taking a look at the Python style guide - writing Python with a conventional style makes it easier for you and other devs to work on and understand. – JRiggles Commented Mar 20 at 15:29