Python Tkinter侧杆

Python Tkinter侧杆,python,tkinter,sidebar,Python,Tkinter,Sidebar,我想创建一个类似上面的可折叠侧栏。 在折叠的表单中,侧栏将仅包含选项的图标,当您将鼠标悬停在侧栏上时,侧栏将展开,显示图标的描述/名称。 如果您单击该图标,它将带您进入指定给该图标的功能,如设置页面。 在右边,我将看到带有许多按钮的主页。 我不知道你会怎么做,所以请帮我开始,剩下的我会处理。 是否也可以仅使用基本的Tkinter进行此操作,或者是否需要通过pip导入更多模块(不可取) 到目前为止,我有: def loginnow(name): global login login.destroy

我想创建一个类似上面的可折叠侧栏。
在折叠的表单中,侧栏将仅包含选项的图标,当您将鼠标悬停在侧栏上时,侧栏将展开,显示图标的描述/名称。
如果您单击该图标,它将带您进入指定给该图标的功能,如设置页面。
在右边,我将看到带有许多按钮的主页。
我不知道你会怎么做,所以请帮我开始,剩下的我会处理。
是否也可以仅使用基本的Tkinter进行此操作,或者是否需要通过pip导入更多模块(不可取)
到目前为止,我有:

def loginnow(name):
global login
login.destroy()
login= Tk()
screen_width = login.winfo_screenwidth()
screen_height = login.winfo_screenheight()
screen_height=str(screen_height)
screen_width=str(screen_width)
screen = screen_width+"x"+screen_height
login.geometry(screen)
login.title("Logged in as %s"%name)
侧栏将包括 设置=
一旦我有了大致的想法,我将使用其他图标进行管理。

我希望菜单不必点击任何东西就能显示出来。

尽管tkinter没有类似的内置功能,但您拥有实现它的所有工具。首先创建一个框架来保存边栏,然后绑定到框架上的
事件以显示和隐藏边栏

至少有三种方式可以显示和隐藏它。例如,如果每个项目都是一个带有图像和文本的按钮,并使用
pack
添加到框架中,则只需添加或删除按钮的文本部分即可使其缩小或缩小

或者,如果使用
grid
并将图标和文本作为单独的小部件创建,则可以使用
grid\u remove
删除第二列中的所有内容,从而导致框架收缩

或者,您可以使用
place
将边栏添加到根窗口,并在显示或隐藏边框时使用
place
更改边框的宽度。

说明: 这实际上可以通过使用绑定来实现。看看下面这个粗略的例子:
from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.geometry('600x600')

min_w = 50 # Minimum width of the frame
max_w = 200 # Maximum width of the frame
cur_width = min_w # Increasing width of the frame
expanded = False # Check if it is completely exanded

def expand():
    global cur_width, expanded
    cur_width += 10 # Increase the width by 10
    rep = root.after(5,expand) # Repeat this func every 5 ms
    frame.config(width=cur_width) # Change the width to new increase width
    if cur_width >= max_w: # If width is greater than maximum width 
        expanded = True # Frame is expended
        root.after_cancel(rep) # Stop repeating the func
        fill()

def contract():
    global cur_width, expanded
    cur_width -= 10 # Reduce the width by 10 
    rep = root.after(5,contract) # Call this func every 5 ms
    frame.config(width=cur_width) # Change the width to new reduced width
    if cur_width <= min_w: # If it is back to normal width
        expanded = False # Frame is not expanded
        root.after_cancel(rep) # Stop repeating the func
        fill()

def fill():
    if expanded: # If the frame is exanded
        # Show a text, and remove the image
        home_b.config(text='Home',image='',font=(0,21))
        set_b.config(text='Settings',image='',font=(0,21))
        ring_b.config(text='Bell Icon',image='',font=(0,21))
    else:
        # Bring the image back
        home_b.config(image=home,font=(0,21))
        set_b.config(image=settings,font=(0,21))
        ring_b.config(image=ring,font=(0,21))

# Define the icons to be shown and resize it
home = ImageTk.PhotoImage(Image.open('home.png').resize((40,40),Image.ANTIALIAS))
settings = ImageTk.PhotoImage(Image.open('settings.png').resize((40,40),Image.ANTIALIAS))
ring = ImageTk.PhotoImage(Image.open('ring.png').resize((40,40),Image.ANTIALIAS))

root.update() # For the width to get updated
frame = Frame(root,bg='orange',width=50,height=root.winfo_height())
frame.grid(row=0,column=0) 

# Make the buttons with the icons to be shown
home_b = Button(frame,image=home,bg='orange',relief='flat')
set_b = Button(frame,image=settings,bg='orange',relief='flat')
ring_b = Button(frame,image=ring,bg='orange',relief='flat')

# Put them on the frame
home_b.grid(row=0,column=0,pady=10)
set_b.grid(row=1,column=0,pady=50)
ring_b.grid(row=2,column=0)

# Bind to the frame, if entered or left
frame.bind('<Enter>',lambda e: expand())
frame.bind('<Leave>',lambda e: contract())

# So that it does not depend on the widgets inside the frame
frame.grid_propagate(False)

root.mainloop()

跟随这个视频:可以,但是有没有一种方法可以在GIF中不使用3行来关闭菜单?哪三行可以关闭菜单。在视频中,Tkinter导航栏左上角|使用Python TkinterYes可以,也许
bind
将整个东西绑定到
这正是我想要的,但是你能在没有PIL的情况下完成吗?@Bond007你需要PIL将图像大小调整为25x25,或者确保你的图标是25x25像素,然后使用更新的代码。好的,谢谢你的帮助,我也一定会使用这个,谢谢你的帮助。
home = PhotoImage(file='home.png') # Make sure the image size is comparable to the minimum frame width
settings = PhotoImage(file='settings.png')
ring = PhotoImage(file='ring.png')