canvas.move 在多帧结构中不工作 class tkinter
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__()
(frame = StartPage(container)
) 中,一个在主代码 (s = StartPage(app)
) 中。绑定在第二个实例上,但未显示。 canvas.move()
正在运行,但您看不到它。
将s = StartPage(app)
更改为s = app.frames[StargPage]
即可解决问题。
你所有的问题是你创建了两次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()
我想编写代码 - 单击我应该移动到下一个屏幕的按钮。我的一个屏幕有一种菜单视图。由于 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__()
(frame = StartPage(container)
) 中,一个在主代码 (s = StartPage(app)
) 中。绑定在第二个实例上,但未显示。 canvas.move()
正在运行,但您看不到它。
将s = StartPage(app)
更改为s = app.frames[StargPage]
即可解决问题。
你所有的问题是你创建了两次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()