似乎无法在 tkinter 中添加背景图片
Can't seem to add background image in tkinter
我似乎无法将背景图片添加到我的代码中。我已经尝试了几乎所有为 tkinter 程序添加背景的解决方案,但仍然无法正常工作。我尝试过的最佳解决方案是下面的代码,它没有给出奇怪的错误,只是根本不起作用。任何帮助将不胜感激,因为我已经为此工作了几个小时而无济于事。
class App:
def __init__(self, root):
#setting title
root.title("undefined")
#setting window size
width=1500
height=900
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
bg = PhotoImage(file="background.png")
bgimg_label=tk.Label(root, image=bg)
bgimg_label["anchor"] = "nw"
bgimg_label.place(x=0,y=0,relwidth=1,relheight=1)
在 class 之外创建图像。
from tkinter import PhotoImage
import tkinter as tk
class App:
def __init__(self, root):
# setting title
root.title("undefined")
# setting window size
width = 1500
height = 900
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
bgimg_label = tk.Label(root, image=bg)
bgimg_label["anchor"] = "nw"
bgimg_label.place(x=0, y=0, relwidth=1, relheight=1)
master = tk.Tk()
bg = PhotoImage(file='background.png')
App(master)
master.mainloop()
我似乎无法将背景图片添加到我的代码中。我已经尝试了几乎所有为 tkinter 程序添加背景的解决方案,但仍然无法正常工作。我尝试过的最佳解决方案是下面的代码,它没有给出奇怪的错误,只是根本不起作用。任何帮助将不胜感激,因为我已经为此工作了几个小时而无济于事。
class App:
def __init__(self, root):
#setting title
root.title("undefined")
#setting window size
width=1500
height=900
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
bg = PhotoImage(file="background.png")
bgimg_label=tk.Label(root, image=bg)
bgimg_label["anchor"] = "nw"
bgimg_label.place(x=0,y=0,relwidth=1,relheight=1)
在 class 之外创建图像。
from tkinter import PhotoImage
import tkinter as tk
class App:
def __init__(self, root):
# setting title
root.title("undefined")
# setting window size
width = 1500
height = 900
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
bgimg_label = tk.Label(root, image=bg)
bgimg_label["anchor"] = "nw"
bgimg_label.place(x=0, y=0, relwidth=1, relheight=1)
master = tk.Tk()
bg = PhotoImage(file='background.png')
App(master)
master.mainloop()