黑框不显示 |特金特



我试图在我的Tkinter应用程序中添加一个框架,但没有效果

from tkinter import *
app=Tk()
screen_width=app.winfo_screenwidth()
screen_height=app.winfo_screenheight()
print(screen_height, screen_width)
app.geometry("%dx%d" % (screen_width,screen_height))

frame=Frame(app, bg='black')
frame.pack()


app.mainloop()

这是代码。黑色框架不显示此代码。我不知道为什么…我是初学者

我玩了一下,在这个链接的帮助下,我发现你需要使用Frame.master.configure(background="black")。我修改了您的示例,并使用random从字符串列表中选择背景颜色,如果您单击按钮。希望这能帮助你开始。另外,我建议你不要使用星形导入,而是独立导入你使用的每个小部件(例如from tkinter import Frame, Label, Tk, Button等..)

from tkinter import *
import random
def change_background_color():
"""Test-function to switch background color of Window object"""
colors = ["black", "blue", "red", "yellow", "orange", "gray", "green"]
app.master.configure(background=random.choice(colors))
# create root object, set window size based on screen size
root=Tk()
root.geometry(f"{root.winfo_screenwidth()}x{root.winfo_screenheight()}")
# create child-frame with background color
app = Frame(root)
app.master.configure(background="black")
# create test-widgets
test_label = Label(root, text="Hello", bg="gray", fg="yellow").pack()
test_button = Button(root, text="Click me", bg="blue", fg="red", command=change_background_color).pack()
root.mainloop()

最新更新