Skip to content Skip to sidebar Skip to footer

Changing A Variablestring In A Class In Python

I have a small piece of code, which was working fine, until I decided to create a class and put things into that class. Now my problem is, I cannot change stringvariable anymore. H

Solution 1:

Add self and and Object, this may fix this error.

import tkinter     as tk
import tkinter.ttk as ttk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        
        frame1 = ttk.LabelFrame(root, text="PANEL A", borderwidth=5)
        frame1.grid(row=0, column=0, padx=5, pady=5)

        frame2 = ttk.LabelFrame(frame1, text="PANEL B", width = 500, height = 1000)
        frame2.grid(row=0, column=0, padx=5, pady=5, sticky='NSWE')

        self.strVarMeasurement = tk.StringVar()

        frame3 = ttk.LabelFrame(frame2, text="PANEL C")
        frame3.grid(row=0, column=0, padx=5, pady=5)
        lbl_01 =    ttk.Label(frame3, width=20, anchor = tk.E, text="Measurement: ").grid(row=0, column=0)
        e_01 =      ttk.Entry (frame3, width=8, textvariable= self.strVarMeasurement).grid(row=0, column=1)

    def setString(self):
        self.strVarMeasurement.set(1234)

if __name__ == "__main__":
    root = tk.Tk()
    app = MainApplication(root)
    app.setString()
    root.mainloop()

Post a Comment for "Changing A Variablestring In A Class In Python"