【发布时间】:2020-09-01 23:57:22
【问题描述】:
我的代码看起来像这样,我不得不说我正在尝试创建的程序将有几个选项卡 (8-10) 和其中的许多小部件,所以我选择使用 .地方()。我一直在尝试显示图片,但没有显示完整,我尝试使用画布和标签,但都显示了部分图片。
这是我的代码
from tkinter import * #GUI
from tkinter import ttk #GUI
from PIL import ImageTk,Image
root = Tk()
root.title("Example") #Title
####get resolution
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#### end get resolution
frame = Frame(root)
frame.pack(fill="both",expand=1)
frame.config(width=screen_width,height=screen_height)
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tabControl.add(tab1, text='Tab 1')
tab2 = ttk.Frame(tabControl)
tabControl.add(tab2, text='Tab 2')
tabControl.place(x=0,y=round(screen_height * .2),
width=round(screen_width),height=round(screen_height * .9))
frame_one = LabelFrame(tab1, text="Image")
frame_one.place(x=round(screen_width * .6),y=round(screen_height *
.01),width=round(screen_width * .3),height=round(screen_height * .3))
frame_two = LabelFrame(tab1, text="Image")
frame_two.place(x=round(screen_width * .6),y=round(screen_height *
.35),width=round(screen_width * .3),height=round(screen_height * .3))
frame_three = LabelFrame(tab1, text="Labels, radios, checkbuttons,
buttons")
frame_three.place(x=round(screen_width * .01),y=round(screen_height *
.01),width=round(screen_width * .55),height=round(screen_height *
.65))
button = Button(tab1,text="Button")
button.place(x=round(screen_width * 0.45), y=round(screen_height *
.66))
canv = Canvas(tab1)
img = ImageTk.PhotoImage(Image.open("test.jpg")) # PIL solution
canv.create_image(0, 0, image=img, anchor=NW)
canv.place(x=round(screen_width * .605),y=round(screen_height * 0.03),
width=round(screen_width * .29), height=round(screen_height * .28))
canv2 = Canvas(tab1)
img2 = ImageTk.PhotoImage(Image.open("test.jpg")) # PIL solution
canv2.create_image(0, 0, image=img2, anchor=NW)
canv2.place(x=round(screen_width * .605),y=round(screen_height *
0.37), width=round(screen_width * .29), height=round(screen_height *
.28))
root.mainloop()
希望有人能帮我把图片完整展示一下
【问题讨论】:
-
既然你在
place(...)中限制了画布的大小,那么大小是否足够显示整个图像? -
place几乎总是比使用pack或grid更难创建响应式 UI。 -
我知道 place() 让它变得更难,但我有几个小部件并且需要以特定方式显示它们,这就是我使用 place 的原因。问题是我无法调整图片的大小,因为用户会选择一张并显示它。
-
但是您可以调整加载图像的大小以适合画布。
-
你能举个例子吗?我试着去做,但我做不到。我是python新手是我的第一个程序
标签: python-3.x image tkinter label