Tkinter Label Class not placing label on window

15 hours ago 1
ARTICLE AD BOX

When calling header_label_sl = make_label(main_window, "Student Login", 20, 300, 10), you are creating an object named header_label_sl of the make_label class. You need to call header_label_sl.make_label() to actually create the label on the GUI.

Complete code:

from tkinter import * class make_label: def __init__(self, place, text, font_size, x_value, y_value): # self.name = name self.place = place self.text = text self.font_size = font_size self.x_value = x_value self.y_value = y_value def make_label(self): new_label = Label(self.place, text=self.text, font=("Century Schoolbook", self.font_size)) new_label.place(x=self.x_value, y=self.y_value) main_window = Tk() main_window.title("University Student Information") main_window.geometry("800x400") header_label_sl = make_label(main_window, "Student Login", 20, 300, 10) header_label_sl.make_label() # <- here main_window.mainloop()

Or, you can just define a function instead of a class:

from tkinter import * def make_label(place, text, font_size, x_value, y_value): new_label = Label(place, text=text, font=("Century Schoolbook", font_size)) new_label.place(x=x_value, y=y_value) main_window = Tk() main_window.title("University Student Information") main_window.geometry("800x400") make_label(main_window, "Student Login", 20, 300, 10) main_window.mainloop()
Read Entire Article