canvas.move在多帧结构类tkinter中无法工作。

问题描述 投票:0回答:1

我想写的代码是--点击按钮我应该被移动到下一个屏幕。我的一个屏幕上有一个菜单式的视图,由于tkinter没有半透明的功能,所以我使用了半透明的图片来聚焦在小组件上。由于tkinter不具有半透明性,我使用了一个半透明的图像,通过将它们绑定到方向键来聚焦在小部件上。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()
python function class tkinter tkinter-canvas
1个回答
1
投票

你所有的问题是你创建的 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()

0
投票

您已经创建了两个 StartPage,一个在里面 App.__init__() (frame = StartPage(container))和主代码中的一个(s = StartPage(app)). 绑定在第二个实例上,但没有显示出来。捆绑在第二个实例上,但没有显示出来。canvas.move() 正在工作,但你看不到它。

改变 s = StartPage(app)s = app.frames[StargPage] 将解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.