[Python] Python Tkinter how to change the text within a frame within a sub window

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    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 = "Georgia"
    ButtonTextFont = "Georgia Bold"
    Backgroundcolor = "Black"
    Bordercolor = "cyan"
    BorderThickness = 4
    ProgramSelectedTextcolor = "green"
    ForgroundFramecolor = "#8ad9ff"
    ForgroundLabelcolor = "#8ad9ff"
    ForgroundButtoncolor = "#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=ForgroundFramecolor , font=(LabelTextFont, 18))
    OPCLabel = tk.Label(OPCFrame, text=" OPCUA Settings " , padx=5, pady=5, bg=DarkMode, fg=ForgroundFramecolor , font=(LabelTextFont, 18))
    OPCcommand_label = tk.Label(OPCFrame, text =" Manual Command :", bg=DarkMode, fg=ForgroundFramecolor , 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=ForgroundFramecolor , font=(LabelTextFont, 12), command = OPCsubmit)
    OPCReturn = tk.Label(OPCFrame, text =" return :", bg=DarkMode, fg=ForgroundFramecolor , 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=ForgroundFramecolor , font=(LabelTextFont, 18))
    NavigationLabel = tk.Label(NavigationFrame, text=" Navigation " , bg=DarkMode, fg=DrManhatanBlue, font=("Georgia", 12))

    NavigationButton3 = tk.Button(NavigationFrame, text=" OPCUA Configuration " , bg=DrManhatanBlue, fg="black", font=("Georgia", 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=ForgroundFramecolor , font=(LabelTextFont, 18))
    DataLabel = tk.Label(DataFrame, text="Data : " , bg=DarkMode, fg=DrManhatanBlue, font=("Georgia", 12))
    Datapopup = tk.Label(DataFrame, text=" " , bg=DarkMode, fg=DrManhatanBlue, font=("Georgia", 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)

    Continue reading...

Compartilhe esta Página