问题描述
我在创建tk.label时遇到麻烦,该标签在我想要的位置有三个按钮。我看不到按钮,也不知道如何通过self.w和self.h来移动它们。
class App(tk.Tk):
def __init__(self,delay):
global image_files
tk.Tk.__init__(self)
self.w = self.winfo_screenwidth()
self.h = self.winfo_screenheight()
self.overrideredirect(1)
self.geometry("%dx%d+0+0" % (self.w,self.h))
self.delay = delay*1000
self.pictures = []
self.setKeyBindings()
self.track_img_ndex = 0
for img in image_files:
self.pictures.append(img)
self.picture_display = tk.Label(self)
self.picture_display.pack(expand=True,fill="both")
frame = tk.Frame(self).pack()
self.boton_izquierdo= tk.Button(frame,text='PrevIoUs picture',command=lambda: move(-1)).pack(side=tk.LEFT)
self.boton_derecho= tk.Button(frame,text='Next picture',command=lambda: move(+1)).pack(side=tk.LEFT)
self.boton_salida= tk.Button(frame,text='Quit',command=self.quit).pack(side=tk.LEFT)`
我想看图片上方的按钮
不是最好的解决方案
up = tk.Frame(root)
up.grid(column=0,row=0,columnspan=3,sticky='n')
s1 = tk.Label(up,text='spacer')
s1.pack()
left = tk.Frame(root)
b1 = tk.Button(left,text='B1')
left.grid(column=0,row=1,sticky='w')
b1.pack()
middle = tk.Frame(root)
middle.grid(column=1,row=1)
s2 = tk.Label(middle,text='spacer')
image = Image.open(image_list[current])
photo = ImageTk.PhotoImage(image)
s2['image'] = photo
s2.photo = photo
s2.pack()
down = tk.Frame(root)
qb = tk.Button(down,text='Exit',command= root.destroy)
down.grid(column=0,row=2,sticky='s')
qb.pack()
right = tk.Frame(root)
right.grid(column=2,sticky='e')
b2 = tk.Button(right,text='B2')
b2.pack()
root.columnconfigure(0,weight=1) #left get space
root.columnconfigure(2,weight=1) #right get space
root.rowconfigure(0,weight=1) #up get space
root.rowconfigure(2,weight=1) #down get space
root.mainloop()
在小部件中放置覆盖物的解决方案。
label = Tkinter.Label(root,compound=Tkinter.TOP)
label.pack()
A=Tkinter.Button(root,command=lambda: move(-1))
A.place(relx=0,x=2,y=20,width=130,height=100)
B=Tkinter.Button(root,command=lambda: move(+1))
B.place(relx=0,x=135,height=100)
C=Tkinter.Button(root,text='PRINT',command=root.quit)
C.place(relx=1,x=-130,height=100)
move(0)
root.update()
root.mainloop()
最后,当您要将按钮放在图像上方时,可以使用place()解决问题。
解决方法
我不知道如何通过self.w和self.h来移动它们
有多种选择,但是由于您希望使用this更容易实现全屏拼接,因此您可以考虑使用layoutmanager place
而不是pack
。
阅读更改链接:
why seperate your layoutmanagement from your initialsation。
import tkinter as tk
class App(tk.Tk):
def __init__(self):
global image_files
tk.Tk.__init__(self)
self.w = self.winfo_screenwidth()
self.h = self.winfo_screenheight()
self.overrideredirect(1)
self.geometry("%dx%d+0+0" % (self.w,self.h))
#self.delay = delay*1000
self.pictures = []
#self.setKeyBindings()
#self.track_img_ndex = 0
#for img in image_files:
#self.pictures.append(img)
#self.picture_display = tk.Label(self)
#self.picture_display.pack(expand=True,fill="both")
self.frame = tk.Frame(self)
self.frame.pack()
self.boton_izquierdo= tk.Button(self.frame,text='Previous picture',command=lambda: move(-1))
self.boton_izquierdo.pack(side=tk.LEFT)
self.boton_derecho= tk.Button(self.frame,text='Next picture',command=lambda: move(+1))
self.boton_derecho.pack(side=tk.LEFT)
self.boton_salida= tk.Button(self.frame,text='Quit',command=self.quit)
self.boton_salida.pack(side=tk.LEFT)
App().mainloop()
,
在小部件中放置覆盖物的解决方案。
label = Tkinter.Label(root,compound=Tkinter.TOP)
label.pack()
A=Tkinter.Button(root,command=lambda: move(-1))
A.place(relx=0,x=2,y=20,width=130,height=100)
B=Tkinter.Button(root,command=lambda: move(+1))
B.place(relx=0,x=135,height=100)
C=Tkinter.Button(root,text='PRINT',command=root.quit)
C.place(relx=1,x=-130,height=100)
move(0)
root.update()
root.mainloop()
最后,当您要将按钮放在图像上方时,可以使用place()解决问题。