[英]Tkinter how to wrap continuous series of images
我是GUI编程的新手。 所以,请问这听起来微不足道。
我正在做一个项目,必须显示与我输入的文字相对应的手语图像。
例如,如果我输入“ Hello”,它应该并排显示手语“ H”,“ E”,“ L”等图像。
问题是万一我输入了长文本,图像从窗口中消失了,甚至无法水平滚动。
我希望在屏幕的整个宽度用完后自动将图像放在新行中。
这是我的代码:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
aimg = ImageTk.PhotoImage(Image.open("path"))
bimg = ImageTk.PhotoImage(Image.open("path"))
# Similarly, I have cimg, dimg, and so on...
def a():
apanel = Label(root, image=aimg)
apanel.pack(side="left", fill="both", expand="yes")
def b():
bpanel = Label(root, image=bimg)
bpanel.pack(side="left", fill="both", expand="yes")
# Similarly, I have functions for all the characters
s = list(input("Enter text here").lower())
for i in s:
if i == 'a':
a()
elif i == 'b':
b()
# So on
root.mainloop()
以下是一些可能有用的屏幕截图:
您可以使用grid
而不是pack
来实现所需的功能。
例如:
r,c = 0,0
def a():
global r,c
if c==5:
r+=1
c=0
apanel = Label(root, image=aimg)
apanel.grid(row=r,column=c)
c+=1
您想要的是响应式网格,为此,您可以使用Tkinter的grid
管理器。
这是一种通过设置最大长度并在达到该长度时在下一行中插入图像的方法:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
aimg = ImageTk.PhotoImage(Image.open("a.jpg").resize((20, 20)))
bimg = ImageTk.PhotoImage(Image.open("b.jpg").resize((20, 20)))
def a():
apanel = Label(root, image=aimg)
apanel.grid(row=r, column=c)
def b():
bpanel = Label(root, image=bimg)
bpanel.grid(row=r, column=c)
# Similarly, I have functions for all the characters
s = list(input("Enter text here: ").lower())
r = 0
c = 0
maxc = 10 # max nr. of columns
for i in s:
if i == 'a':
a()
elif i == 'b':
b()
c+=1
if (c==maxc): # when you reach the last column, go to the next row
c = 0
r += 1
# So on
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.