in the following the canvas does not appear when i click on the button: import tkinter…
- User
in the following the canvas does not appear when i click on the button: import tkinter as tk from tkinter import font
class TextField(tk.Text): def init(self, *args, **kwargs): super().init(*args, **kwargs) self.font = font.Font(size=24) self.config(font=self.font) self.bind("", self.on_resize)
def on_resize(self, event): # Calculate the new font size based on the window size new_size = -max(1, int(self.w / self.font.measure(" "))) self.font.config(size=new_size) self.config(font=self.font)
class CanvasField(tk.Canvas): def init(self, *args, **kwargs): super().init(*args, **kwargs)
class Application(tk.Tk): def init(self): super().init() self.title("Text Field / Canvas")
codeself.text_field = TextField(self) self.canvas_field = CanvasField(self) self.text_field.pack(side="top", fill="both", expand=True) self.canvas_field.pack(side="top", fill="both", expand=True) self.text_field.pack_forget() self.canvas_field.pack(side="top", fill="both", expand=True) self.text_button = tk.Button(self, text="Text Field", command=self.switch_to_text) self.canvas_button = tk.Button(self, text="Canvas", command=self.switch_to_canvas) self.text_button.pack(side="bottom", fill="x") self.canvas_button.pack(side="bottom", fill="x")def switch_to_text(self): self.text_field.pack(side="top", fill="both", expand=True) self.canvas_field.pack_forget() self.text_button.config(state="disabled") self.canvas_button.config(state="normal")
def switch_to_canvas(self): self.text_field.pack_forget() self.canvas_field.pack(side="top", fill="both", expand=True) self.text_button.config(state="normal") self.canvas_button.config(state="disabled")
if name == "main": app = Application() app.mainloop()
- Assistant