gpt4 book ai didi

python - 如何使用 Tkinter python 2.7 将背景图像设置为窗口

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:38 25 4
gpt4 key购买 nike

我正在努力设置背景图像,并在该图像上添加标签、按钮和所有内容。一切都来了,但不是在背景图片上,它是这样的:

enter image description here

我的代码是:

from Tkinter import Tk, Frame, BOTH
import Tkinter
from PIL import Image, ImageTk

class Example(Frame):

def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()

def initUI(self):

self.parent.title("PISE")
self.pack(fill=BOTH, expand=1)

root = Tk()
root.geometry("1111x675+300+300")
app = Example(root)

im = Image.open('wood.png')
tkimage = ImageTk.PhotoImage(im)
Tkinter.Label(root,image = tkimage).pack()

custName = StringVar(None)
yourName = Entry(app, textvariable=custName)
yourName.pack()

relStatus = StringVar()
relStatus.set(None)

labelText = StringVar()
labelText.set('Accuracy Level')
label1 = Label(app, textvariable=labelText, height=2)
label1.pack()

radio1 = Radiobutton(app, text='100%', value='1', variable = relStatus, command=beenClicked1).pack()
radio2 = Radiobutton(app, text='50%', value='5', variable = relStatus, command=beenClicked5).pack()

root.mainloop()

如何让背景图片合适?

提前致谢!

最佳答案

你应该使用 place()用于放置图像,然后使用 grid()(我个人更喜欢网格)或用于其他小部件的 pack()

from Tkinter import Tk, Frame, BOTH
import Tkinter
from PIL import Image, ImageTk

class Example(Frame):

def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()

def initUI(self):
self.parent.title("PISE")
self.pack(fill=BOTH, expand=1)

root = Tk()
root.geometry("1111x675+300+300")
app = Example(root)

im = Image.open('Test1.png')
tkimage = ImageTk.PhotoImage(im)
myvar=Tkinter.Label(root,image = tkimage)
myvar.place(x=0, y=0, relwidth=1, relheight=1)

custName = StringVar(None)
yourName = Entry(root, textvariable=custName)
yourName.pack()

relStatus = StringVar()
relStatus.set(None)

labelText = StringVar()
labelText.set('Accuracy Level')
label1 = Label(root, textvariable=labelText, height=2)
label1.pack()

def beenClicked1():
pass

def beenClicked5():
pass

radio1 = Radiobutton(root, text='100%', value='1', variable = relStatus, command=beenClicked1).pack()
radio2 = Radiobutton(root, text='50%', value='5', variable = relStatus, command=beenClicked5).pack()

root.mainloop()

小部件不可见的原因是因为您使用了两个不同的 parents,即 app(它是 InstanceExample,所以不要使用这个)和root

关于python - 如何使用 Tkinter python 2.7 将背景图像设置为窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22727524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com