【发布时间】:2020-02-27 20:10:47
【问题描述】:
喂! 我正在尝试在 Tkinter 中创建的 GUI 中使用图像作为背景。在这个项目的不同框架上使用相同的代码可以正常工作,但它不想在不同的框架中工作。我没有错误,框架只是空白。 感谢您的帮助!
这里我可以用图片做背景就好了:
def main_screen():
global screen
screen = Tk()
screen.geometry("600x750")
screen.configure(background="#022140")
screen.title("Hermes")
filename = PhotoImage(file="background.png")
filename_small = filename.subsample(2, 2)
background_label = Label(image=filename_small)
background_label.place(x=1, y=1, relwidth=1, relheight=1)
login_button = Button(text="Login", bg="#022140", height="2", width="30", command=login,
highlightbackground='#494B68')
screen.mainloop()
但在这里它不起作用:
def login_sucess():
global screen3
screen3 = Toplevel(screen)
screen3.geometry("500x400")
filename = PhotoImage(file="main_theme.png")
filename_small = filename.subsample(2, 2)
background_label = Label(screen3, image=filename_small)
background_label.place()
感谢您的帮助!
【问题讨论】:
-
对于第一种情况,
screen.mainloop()在main_screen()内部调用了吗?如果否,则不应显示背景图片(与第二种情况相同)。 -
我试过了,没有任何改变。
-
你尝试了什么?在
main_screen()内部或外部调用screen.mainloop()?正如我所说,如果在函数内部调用图像将显示图像,但如果在函数外部调用则不会显示。 -
我尝试在函数 def login_success() 中调用 screen3.mainloop():
-
对于第二种情况,最好不要调用
screen3.mainloop(),因为已经有一个主循环(screen.mainloop())在运行。
标签: python image tkinter background