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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22727524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:30:24  来源:igfitidea点击:

how to set background image to a window using Tkinter python 2.7

pythonpython-2.7tkinter

提问by varsha_holla

I was working on setting the background image and upon that image adding the labels, buttons and all. everything is coming, but not on the background image, its like this:

我正在设置背景图像,并在该图像上添加标签、按钮等。一切都来了,但不是在背景图片上,就像这样:

enter image description here

在此处输入图片说明

And my code is:

我的代码是:

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()  

How to fit the background image properly?

如何正确适应背景图像?

Thanks in advance!

提前致谢!

采纳答案by Alok

You should use place()for placing the image & then use grid()(personally i prefer grid) or pack()for other widgets.

您应该使用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()

The reason the widgets were not visible was because you were using two different parents,i.e, app(its an Instanceof class Example,so don't use this) and root.

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