Tkinter Python中的弹出滑块

接线不同

是否可以在tkinter python中创建弹出滑块(或任何小部件)?

像这个例子,但有滑块吗?

它应该看起来像这样(忽略背景),最好在单击以弹出的按钮上方

注意:
我正在寻找POP,所以请不要建议我更改当前布局以添加滑块

布莱恩·奥克利(Bryan Oakley)

是的,有可能。但是,您必须自己使用框架来构建它。这是其中一个很好的例子place是优于gridpack自部件的放置不会影响其他部件。

关键是创建一个框架,该框架是根窗口的子级,以便该框架将显示在其他窗口小部件上方。然后,只需调用place并使用in_参数来指定框架位置应相对于按钮即可。

这是一个实现为类的基本示例:

import tkinter as tk

class PopupSliderButton(tk.Button):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        self.configure(command=self.toggle)
        self.sliderframe = tk.Frame(self.winfo_toplevel(), bd=1, relief="sunken", bg="#ebebeb")
        self.slider = tk.Scale(self.sliderframe, from_=0, to_=100, background=self.sliderframe.cget("background"))
        self.slider.pack(fill="both", expand=True, padx=4, pady=4)

    def get(self):
        return self.slider.get()

    def toggle(self):
        if self.sliderframe.winfo_viewable():
            self.sliderframe.place_forget()
        else:
            self.sliderframe.place(in_=self, x=0, y=-4, anchor="sw")

root = tk.Tk()
root.geometry("200x300")

text = tk.Text(root, bd=1, relief="sunken", highlightthickness=0)
button_frame = tk.Frame(root)

button_frame.pack(side="bottom", fill="x")
text.pack(side="top", fill="both", expand=True, padx=2, pady=2)

select_button = tk.Button(button_frame, text="select")
pop_up_button = PopupSliderButton(button_frame, text="pop_up")

select_button.pack(side="left")
pop_up_button.pack(side="right")


text.insert("end", "line 3\nline 2\nline1")

root.mainloop()

屏幕截图

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章