如何在 Tkinter 中将图像放在同一个 window 中?
How do I place an image in the same window in Tkinter?
我试图将图像放在同一个 window 中,但它会在新的 window 中打开,我不明白为什么。我试过 google 但找不到任何帮助我的东西。我如何指定图像应该放在打开的初始 window 中而不是它自己单独的 window?
import Tkinter as tk
import io
import base64
# Import the function for downloading web pages
from urllib import urlopen
# Import the regular expression function
from re import findall
# Import the Tkinter functions
from Tkinter import *
# Import Python's HTML parser
from HTMLParser import *
class MainWindow(tk.Frame):
root = tk.Tk()
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def createimage(self):
# a little more than width and height of image
w = 520
h = 320
x = 80
y = 100
# use width x height + x_offset + y_offset (no spaces!)
# this GIF picture previously downloaded from tinypic.com
image_url = "http://i46.tinypic.com/r9oh0j.gif"
image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
self.photo = tk.PhotoImage(data=image_b64)
# create a white canvas
#topframe = Frame(root)
cv = tk.Canvas(bg='white')
cv.pack(side='top', expand='yes')
# put the image on the canvas with
# create_image(xpos, ypos, image, anchor)
cv.create_image(10, 10, image=self.photo, anchor='nw')
if __name__ == "__main__":
root = tk.Tk()
main = MainWindow(root)
root.geometry("400x300")
main.pack(side="top", fill="both", expand=True)
main.createimage()
root.mainloop()
1。为什么它在单独的 window 中打开?
你需要给每个构造器传递一个master。这应该是新小部件应该放在里面的小部件(Tk,Frame,Toplevel,Canvas,...)。像这样:
tk.Canvas(master = self, g='white')
tk.PhotoImage(master = self, data=image_b64)
当master被销毁时,以它为master的widgets也会被销毁。
2。在哪个window打开?
Tkinter 有默认根目录,第一个 window 创建。此 window 用于您未将母版传递给的任何小部件。
即第一个root = tk.Tk()
我试图将图像放在同一个 window 中,但它会在新的 window 中打开,我不明白为什么。我试过 google 但找不到任何帮助我的东西。我如何指定图像应该放在打开的初始 window 中而不是它自己单独的 window?
import Tkinter as tk
import io
import base64
# Import the function for downloading web pages
from urllib import urlopen
# Import the regular expression function
from re import findall
# Import the Tkinter functions
from Tkinter import *
# Import Python's HTML parser
from HTMLParser import *
class MainWindow(tk.Frame):
root = tk.Tk()
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def createimage(self):
# a little more than width and height of image
w = 520
h = 320
x = 80
y = 100
# use width x height + x_offset + y_offset (no spaces!)
# this GIF picture previously downloaded from tinypic.com
image_url = "http://i46.tinypic.com/r9oh0j.gif"
image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
self.photo = tk.PhotoImage(data=image_b64)
# create a white canvas
#topframe = Frame(root)
cv = tk.Canvas(bg='white')
cv.pack(side='top', expand='yes')
# put the image on the canvas with
# create_image(xpos, ypos, image, anchor)
cv.create_image(10, 10, image=self.photo, anchor='nw')
if __name__ == "__main__":
root = tk.Tk()
main = MainWindow(root)
root.geometry("400x300")
main.pack(side="top", fill="both", expand=True)
main.createimage()
root.mainloop()
1。为什么它在单独的 window 中打开?
你需要给每个构造器传递一个master。这应该是新小部件应该放在里面的小部件(Tk,Frame,Toplevel,Canvas,...)。像这样:
tk.Canvas(master = self, g='white')
tk.PhotoImage(master = self, data=image_b64)
当master被销毁时,以它为master的widgets也会被销毁。
2。在哪个window打开?
Tkinter 有默认根目录,第一个 window 创建。此 window 用于您未将母版传递给的任何小部件。
即第一个root = tk.Tk()