I’m trying to learn some basic ttkbootstrap/tkinter in Python, and I’ve got a small scenario I can’t seem to understand and work around.
I have the following “Demo” code below, which is loosely pieced together with some samples I found online.
import ttkbootstrap as tb
from tkinter import *
from ttkbootstrap.constants import *
class MyApp(tb.Frame):
global appname, appver
appname = "Test App"
appver = "0.1B"
def __init__(self, master):
super().__init__(master)
self.pack(fill=BOTH, expand=YES, padx=20, pady=20)
# Variables
self.TextBox1_var = tb.StringVar(value='')
# Label Frame builder
option_text = "Test"
self.option_lf = tb.Labelframe(self, text=option_text, padding=5)
self.option_lf.pack(fill=X, expand=YES, anchor=N)
# Build the form
self.TextBox1_Row()
self.Button1_Row()
def TextBox1_Row(self):
"""Add row to labelframe"""
TextBox1_row = tb.Frame(self.option_lf)
TextBox1_row.pack(fill=X, expand=YES, pady=5)
TextBox1_lbl = tb.Label(TextBox1_row, text="Input:", width=12)
TextBox1_lbl.pack(side=LEFT, padx=(15, 0))
TextBox1_ent = tb.Entry(TextBox1_row, textvariable=self.TextBox1_var)
TextBox1_ent.pack(side=LEFT, fill=X, expand=YES, padx=5)
self.TextBox1_var.trace_add("write", lambda name, index, mode, sv=self.TextBox1_var: MonitorTextChanges.TextBox1_callback(self.TextBox1_var))
def Button1_Row(self):
"""Add row to labelframe"""
Button1_Row = tb.Frame(self.option_lf)
Button1_Row.pack(fill=X, expand=YES, pady=5)
Button1_Go = tb.Button(
master=Button1_Row,
text="Go",
width=15,
state=DISABLED
)
Button1_Go.pack(side=RIGHT, padx=5)
class MonitorTextChanges():
def TextBox1_callback(TextBox1_var):
if len(TextBox1_var.get()) != 0:
print(TextBox1_var.get())
#MyApp.Button1_Row.Button1_Go.config(state="normal")
else:
print("Please enter some text!")
#MyApp.Button1_Row.Button1_Go.config(state="disabled")
def main():
app = tb.Window(
title=appname + " - " + appver,
themename="superhero",
minsize=(300,150),
resizable=(True, True),
scaling=2
)
MyApp(app)
MonitorTextChanges()
app.mainloop()
if __name__ == '__main__':
main()
I’m attempting to update the state of the button whenever there is present text, and to disable it whenever the text field is empty from between classes, but I cannot seem to figure out how to target the button in the MyApp
class.
I’ve tried the fllowing:
MyApp.Button1_Go.config(state="normal")
which yields
AttributeError: type object 'MyApp' has no attribute 'Button1_Go'. Did you mean: 'Button1_Row'?
MyApp.Button1_Row.config(state="normal")
which yields
AttributeError: 'function' object has no attribute 'config'
MyApp.Button1_Row.Button1_Go.config(state="normal")
which yields
AttributeError: 'function' object has no attribute 'Button1_Go'
So, I’m not sure how to actually target this button in the MyApp
class to change the state of it.