Tkinter的 Label控件没有把标签放到窗口上
我正在尝试创建一个类,将tkinter的标题标签放到一个窗口上,但不知为何它们无法放置。
我已经尝试把.place的调用移出类外,或者调整它的位置,但似乎都不起作用。
我更愿意使用place,因为它是我最熟悉的方法。
该类和窗口位于不同的文件中,且该类已经被导入。
以下是该类的代码
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)
以下是窗口的代码
from tkinter import *
from Classes_File_2503056 import *
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)
main_window.mainloop()
解决方案
The issue is that you are creating the make_label object but not calling the make_label method which actually creates and places the label. You need to call header_label_sl.make_label() after creating the header_label_sl object.
from tkinter import *
from Classes_File_2503056 import *
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)
# Call the method to create and place the label
header_label_sl.make_label()
main_window.mainloop()
In your original code, you were initializing the make_label object but never actually telling it to create the label widget and place it on the window. Do this by adding header_label_sl.make_label(): the make_label method creates the Label and uses .place() to put it on the main_window.
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。