Working on displaying some information inside a tkinter 'Toplevel' window. I want the text size to increase as you increase the size of the window. For this I am just using the label widget inside a toplevel widget.
I've made a function that triggers during a resize event of the window, and update a global font variable.
def showResults(self):
self.top = Toplevel(master = None, height=750,width=750, relief="sunken")
self.top.title("Results")
self.font = ("Times", 20, "bold")
self.fontSize = IntVar()
self.fontSize.set(20)
self.resultX = Label(self.top, text="Name: X", font=("Times", self.fontSize.get(), "bold")).pack(side = "left", fill = "both", expand=True)
self.top.bind('<Configure>', self.resize)
print(self.fontSize.get())
def resize(self, event):
self.fontSize.set(self.top.winfo_height())
I expected self.fontSize to update once resize is called, however, it isn't being updated. If I move that print statement in showResults to resize however, it does print the updated size. It's just not refreshing the label widget. Anyone know of a simple way to perhaps 'refresh' label widgets to display the proper information? Thank you!
You can do this by defining a font.Font
object and set it as the font of your Label
widget:
from tkinter import *
from tkinter import font
class Body(Frame):
def __init__(self,master=None,**kwargs):
super().__init__(master,**kwargs)
self.button = Button(self,text="Click",command=self.showResults)
self.button.pack()
def showResults(self):
self.font = font.Font(self.master, family="Times", size=20, weight="bold")
self.top = Toplevel(master=None, height=750, width=750, relief="sunken")
self.top.title("Results")
self.resultX = Label(self.top, text="Name: X", font=self.font)
self.resultX.pack(side="left",fill="both",expand=True)
self.update()
self.top.bind('<Configure>', self.resize)
def resize(self, event):
self.font['size'] = self.top.winfo_height()
root = Tk()
Body(root).pack()
root.mainloop()