【问题标题】:What is the problem of the sidebar and how to fix it?侧边栏有什么问题以及如何解决?
【发布时间】:2023-01-29 10:55:25
【问题描述】:

我正在尝试使用和绑定来制作侧边栏,如果我将鼠标移动得更慢,一切正常,但如果我使用鼠标移动得更快,侧边栏就会开始不停地来回移动。有办法解决吗?

(一般来说,我是 tkinter 和 python 的新手) 1/2 2/2

from tkinter import *

#主窗口 定义主窗口(): 全局窗口主

    WindowMain = Tk()
    WindowMain.config(background="LightGray")
    WindowMain.overrideredirect(1)


#MainWindow_size
def Screen_size():
    app_width = 1280
    app_height = 720

    screen_width = WindowMain.winfo_screenwidth()
    screen_height = WindowMain.winfo_screenheight()

    x = (screen_width / 2) - (app_width / 2)
    y = (screen_height / 2) - (app_height / 2)
    WindowMain.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')

#Func1
def close(e):
    for x in range(1000, 1200):
        Blue.place(x=x, y=0)
        DarkBlue.place(x=x, y=0)
        Blue.update()
        DarkBlue.update()
        Blue.bind("<Enter>", open)

#Func2
def open(e):
    for x in range(-1200, -1000):
        Blue.place(x=-x, y=0)
        DarkBlue.place(x=-x, y=0)
        Blue.update()
        DarkBlue.update()
        Blue.bind("<Leave>", close)


MainWindow()
Screen_size()

#Label1&2
Blue = Label(WindowMain, background="DeepSkyBlue",
             width=70, height=50)
DarkBlue = Label(WindowMain, width=2, height=100,
                 background="DodgerBlue")

Blue.place(x=1200)
DarkBlue.place(x=1200)

#Buttons
Quit = Button(WindowMain, text="Quit", command=quit,
              background="LightSkyBlue").pack()

Move = Button(WindowMain, text="open", command=open,
              background="LightSkyBlue", state=DISABLED).pack()
Undo = Button(WindowMain, text="close", command=close,
              background="LightSkyBlue", state=DISABLED).pack()


Blue.bind("<Enter>", open)


WindowMain.mainloop()

【问题讨论】:

  • 你能把代码贴出来吗?我们需要查看代码才能提供帮助
  • 你可以点击照片链接来查看
  • 请在问题正文中包含代码而不是屏幕截图。
  • 如果您阅读How to ask 它说:不要张贴代码、数据、错误消息等的图像 - 将文本复制或键入问题中。
  • 查看after方法并避免使用循环和update

标签: python tkinter


【解决方案1】:

您的问题将得到解决。

  • 我只是玩玩Blue.update()Blue.bind。见行 31-31 和 40-41。
  • 在第 31-31 行和第 40-41 行中,应该在循环之外。
  • 不要使用关键字openclose。这些对 Python 3 有效

修改后的代码:

from tkinter import *
#MainWindow
#def MainWindow():
    #global WindowMain

WindowMain = Tk()
WindowMain.config(background="LightGray")
#WindowMain.overrideredirect(1)


#MainWindow_size
def Screen_size():
    app_width = 1280
    app_height = 720

    screen_width = WindowMain.winfo_screenwidth()
    screen_height = WindowMain.winfo_screenheight()

    x = (screen_width / 2) - (app_width / 2)
    y = (screen_height / 2) - (app_height / 2)
    WindowMain.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')

#Func1
def _close(e):
    for x in range(1000, 1200):
        Blue.place(x=x, y=0)
        DarkBlue.place(x=x, y=0)         
         
    Blue.update()    
    Blue.bind("<Enter>", _open)

#Func2
def _open(e):
    for x in range(-1200, -1000):
        Blue.place(x=-x, y=0)
        DarkBlue.place(x=-x, y=0)
                 
    Blue.update() 
    Blue.bind("<Leave>", _close)

Screen_size() 

#Label1&2
Blue = Label(WindowMain, background="DeepSkyBlue",
             width=70, height=50)
DarkBlue = Label(WindowMain,width=2, height=100,
                 background="DodgerBlue")

Blue.place(x=1200)
DarkBlue.place(x=1200)
 
#Buttons
Quit = Button(WindowMain, text="Quit", command=quit,
              background="LightSkyBlue").pack()

Move = Button(WindowMain, text="open", command=_open,
              background="LightSkyBlue", state=DISABLED).pack()
Undo = Button(WindowMain, text="close", command=_close,
              background="LightSkyBlue", state=DISABLED).pack()
  
Blue.bind("<Enter>", _open)
     
WindowMain.mainloop()

鼠标移动前截图:

鼠标移动后截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2016-04-13
    相关资源
    最近更新 更多