이미지 위에 버튼을 놓고 창 중앙에 유지하려면 어떻게해야합니까? 코드는 잘 작동하지만 버튼은 이미지 아래에 있으며 클릭 할 수 없습니다.
from tkinter import Frame, Tk, Label, Button
from PIL import Image, ImageTk
class Example(Frame):
def __init__(self, master, *pargs):
Frame.__init__(self, master, *pargs)
self.image = Image.open("folder\\file.gif")
self.img_copy= self.image.copy()
self.background_image = ImageTk.PhotoImage(self.image)
self.background = Label(self, image=self.background_image)
self.background.pack(fill="both", expand="YES")
self.background.bind('<Configure>', self._resize_image)
def _resize_image(self,event):
new_width = event.width
new_height = event.height
self.image = self.img_copy.resize((new_width, new_height))
self.background_image = ImageTk.PhotoImage(self.image)
self.background.configure(image = self.background_image)
root = Tk()
root.title("Title")
root.geometry("600x600")
root.configure(background="black")
e = Example(root)
e.pack(fill="both", expand="YES")
btn=Button(root,text="Hello World").pack()
root.mainloop()
이것이 실제 앱에 적합한 솔루션인지 모르겠지만 질문의 코드에 대해 가장 간단한 솔루션은 place
. 와 함께 place
상대 배치를 사용하여 위젯을 다른 위젯의 중앙에 유지할 수 있습니다.
place
범용 레이아웃 관리자 로서도 최고는 아니지만 매우 구체적인 사용 사례에서는 작업에 적합한 도구입니다. 다음 예에서, 버튼은 중앙에 위치 Example
하여 프레임 relx
, rely
및 anchor
. in_
상대 좌표 위젯 파라미터 지정에 상대적이다.
btn=Button(root,text="Hello World")
btn.place(in_=e, relx=.5, rely=.5, anchor="c")
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다