【问题标题】:tKinter switch color on hotkey clicktKinter 在热键单击时切换颜色
【发布时间】:2022-01-16 12:45:47
【问题描述】:

我有一个程序可以打开一个窗口,并在开始时在中心填充一个绿色的圆圈。每次按下按钮(此代码中的 r)时,我希望这个圆圈从红色变为绿色,从绿色变为红色,但是我只能更改一次颜色

from tkinter import *
import keyboard

if __name__ == '__main__':
    window = Tk()

    window.title("On Record")
    window.configure(width=500, height=300)
    window.configure(bg='lightgray')
    myCanvas = Canvas(window)
    myCanvas.pack()


    def daire(x, y, r, canvasName, color):  # center coordinates, radius
        x0 = x - r
        y0 = y - r
        x1 = x + r
        y1 = y + r
        return canvasName.create_oval(x0, y0, x1, y1, fill=color)


    # move window center
    winWidth = window.winfo_reqwidth()
    winwHeight = window.winfo_reqheight()
    posRight = int(window.winfo_screenwidth() / 2 - winWidth / 2)
    posDown = int(window.winfo_screenheight() / 2 - winwHeight / 2)
    window.geometry("+{}+{}".format(posRight, posDown))
    color = "green"

    def onKeyPress(event):
        print("pressed")
        counter = 0
        counter = counter + 1
        print(counter)
        if counter % 2 == 0:
            color = "green"
        else:
            color = "red"
        daire(190, 135, 120, myCanvas, color)


    window.bind("r", onKeyPress)
    daire(190, 135, 120, myCanvas, "green")
    window.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    问题是事件函数依赖于奇数或偶数计数器,但每次运行时它都会自动将其重置为 0。一种解决方案是将计数器放在函数之外并将其作为全局变量调用。

    from tkinter import *
    import keyboard
    
    if __name__ == '__main__':
        window = Tk()
    
        window.title("On Record")
        window.configure(width=500, height=300)
        window.configure(bg='lightgray')
        myCanvas = Canvas(window)
        myCanvas.pack()
    
        def daire(x, y, r, canvasName, color):  # center coordinates, radius
            x0 = x - r
            y0 = y - r
            x1 = x + r
            y1 = y + r
            return canvasName.create_oval(x0, y0, x1, y1, fill=color)
    
        # move window center
        winWidth = window.winfo_reqwidth()
        winwHeight = window.winfo_reqheight()
        posRight = int(window.winfo_screenwidth() / 2 - winWidth / 2)
        posDown = int(window.winfo_screenheight() / 2 - winwHeight / 2)
        window.geometry("+{}+{}".format(posRight, posDown))
        color = "green"
    
        counter = 0
    
        def onKeyPress(event):
            global counter
            print("pressed")
            counter += 1
            print(counter)
            if counter % 2 == 0:
                color = "green"
            else:
                color = "red"
            daire(190, 135, 120, myCanvas, color)
    
    
        window.bind("r", onKeyPress)
        daire(190, 135, 120, myCanvas, color)
        window.mainloop()
    

    出于兴趣,您是否真的需要能够跟踪按下 r 的次数?它由您的脚本暗示,但未明确说明。否则,通过直接引用 tkinter 元素可以更简化地执行此操作,这通常通过按钮切换来完成 as displayed in this post.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2021-03-28
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      相关资源
      最近更新 更多