[英]canvas.move is not working in multiple frame structured class tkinter
我想編寫代碼 - 單擊我應該移動到下一個屏幕的按鈕。 我的一個屏幕有一種菜單視圖。 由於 tkinter 沒有半透明,我使用半透明圖像通過將它們綁定到箭頭鍵來聚焦在小部件上。 代碼在沒有 class 的情況下工作,但是當我在這個 class 中使用時,綁定鍵正在工作並且它們打印值但是我的canvas.move
不工作......我的意思是半透明圖像沒有移動相鄰的小部件。
from tkinter import *
from PIL import Image
from PIL import ImageTk
class App(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack()
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = StartPage(container)
self.frames[StartPage] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, context):
frame = self.frames[context]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.image1 = PhotoImage(file=r'images/iot.png')
self.image2 = PhotoImage(file=r'images/facialExp.png')
self.image3 = PhotoImage(file=r'images/cursor.png')
self.image4 = PhotoImage(file=r'images/mindR.png')
self.imgg = PhotoImage(file=r'images/arrow.png')
self.canvas = Canvas(width=2085, height=1080, bg='#020A2E')
self.canvas.pack()
label = Label(text="FEATURES", bg='#020A2E', fg='white', font='Arial 50 bold').place(x=80, y=20)
arrow = Button(width=40, height=30, bg='#020A2E', image=self.imgg, bd=0).place(x=10, y=10)
button1 = Button(width=200, height=215, bg="#3A3535", bd=0, image=self.image1).place(x=380, y=150)
self.canvas.create_rectangle(75, 150, 380, 365, fill='#615A5A')
self.canvas.create_text(220, 160, text="TURN ON THE LIGHTS", anchor=N, font='Arial 14 bold', fill='white')
# label1= Label(root,text="TURN ON THE LIGHTS",fg='yellow',font='Arial 10 bold').place(x=100,y=160)
# label11 = Label(root,text="just move your head towards left and blink\nyour eye twice to open this feature or tap\nthe icon if you are using the cursor",fg='yellow',font='Arial 10 bold').place(x=90,y=200)
# canvas.create_rectangle(50,130,610,390,fill='#FFFFFF',stipple="gray12") #hover on tile1
button2 = Button(width=200, height=215, bg="#3A3535", bd=0, image=self.image2).place(x=1100, y=150)
self.canvas.create_rectangle(795, 150, 1101, 368, fill='#615A5A')
label2 = Label(text="TURN ON THE LIGHTS", fg='yellow', bg='#615A5A', font='Arial 10 bold').place(x=900,
y=160)
# canvas.create_rectangle(770, 130,1325,390, fill='#FFFFFF')
button3 = Button(width=200, height=215, bg="#3A3535", bd=0, image=self.image3).place(x=380, y=450)
self.canvas.create_rectangle(75, 450, 380, 667, fill='#615A5A')
# canvas.create_rectangle(50,430,607,690,fill='#FFFFFF')
button4 = Button(width=200, height=215, bg="#3A3535", bd=0, image=self.image4).place(x=1100, y=450)
self.canvas.create_rectangle(795, 450, 1100, 669, fill='#615A5A')
# canvas.create_rectangle(770,430,1325,688,fill='#FFFFFF')
self.img = Image.open("images/sky.png")
self.my_img = ImageTk.PhotoImage(self.img)
self.my_rectangle = self.canvas.create_image(330, 255, image=self.my_img, tags='close_tag')
# my_rectangle=canvas.create_rectangle(50,130,610,390,fill="#FFFFFF", stipple="gray12")
def left(self, event):
x = -720
y = 0
# xx=event.xx
# yy=event.yy
pos = self.canvas.coords('close_tag')
if pos == [1050.0, 255.0] or pos == [1050.0, 555.0]:
print('left', pos)
self.canvas.move(self.my_rectangle, x, y)
def right(self, event):
x = 720
y = 0
pos = self.canvas.coords('close_tag')
if pos == [330.0, 255.0] or pos == [330.0, 555.0]:
print('right', pos)
self.canvas.move(self.my_rectangle, x, y)
def up(self, event):
x = 0
y = -300
pos = self.canvas.coords('close_tag')
if pos == [330.0, 555.0] or pos == [1050.0, 555.0]:
print('up', pos)
self.canvas.move(self.my_rectangle, x, y)
def down(self, event):
x = 0
y = 300
pos = self.canvas.coords('close_tag')
if pos == [330.0, 255.0] or pos == [1050.0, 255.0]:
print('down', pos)
self.canvas.move(self.my_rectangle, x, y)
app = App()
app.geometry('2085x1080')
s = StartPage(app)
app.bind("<Left>", s.left)
app.bind("<Right>", s.right)
app.bind("<Up>", s.up)
app.bind("<Down>", s.down)
app.mainloop()
您所有的問題是您兩次創建StartPage
在App.__init__
中創建StartPage
frame = StartPage(container)
並顯示出來。
但稍后您創建第二個StartPage
s = StartPage(app)
未顯示,但您將鍵綁定到此StartPage
您可以在StartPage.__init__
中綁定鍵
class StartPage(Frame):
def __init__(self, parent):
# ... code...
parent.master.bind("<Left>", self.left)
parent.master.bind("<Right>", self.right)
parent.master.bind("<Up>", self.up)
parent.master.bind("<Down>", self.down)
並且只運行
app = App()
app.geometry('2085x1080')
app.mainloop()
您已經創建了StartPage
的兩個實例,一個在App.__init__()
( frame = StartPage(container)
) 中,一個在主代碼 ( s = StartPage(app)
) 中。 綁定在第二個實例上,但未顯示。 canvas.move()
正在運行,但您看不到它。
將s = StartPage(app)
更改為s = app.frames[StargPage]
解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.