Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter Toplevel“;下拉列表“;注意力不集中_Python_Tkinter - Fatal编程技术网

Python Tkinter Toplevel“;下拉列表“;注意力不集中

Python Tkinter Toplevel“;下拉列表“;注意力不集中,python,tkinter,Python,Tkinter,我试图在Tkinter中创建一个自定义的下拉列表,我决定使用它。除焦点未发生外,一切正常。创建窗口时,它会停留在我的其他窗口的顶部,但在我单击窗口之前,/绑定不起作用。这是一个精确问题的重建示例 import tkinter as tk from tkinter import ttk class App(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.mainframe = ttk.Fram

我试图在Tkinter中创建一个自定义的下拉列表,我决定使用它。除焦点未发生外,一切正常。创建窗口时,它会停留在我的其他窗口的顶部,但在我单击窗口之前,
/
绑定不起作用。这是一个精确问题的重建示例

import tkinter as tk
from tkinter import ttk


class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.mainframe = ttk.Frame(self)

        self.init_ui()
        self.mainloop()

    def init_ui(self):
        self.mainframe.grid(row=0, column=0, sticky='nsew')
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)

        self.mainframe.rowconfigure(0, weight=1)
        self.mainframe.columnconfigure(0, weight=1)

        l = ttk.Label(self.mainframe, text='Test ▾')
        l.config(cursor='hand', font=('Helvetica', 12, 'underline'))
        l.bind('<Button-1>', self.dropdown)
        l.grid(row=0, column=0, padx=50, pady=(5, 300), sticky='new')

    def dropdown(self, *args):
        top = tk.Toplevel(self)
        top.overrideredirect(1)
        top.transient(self)

        def create_label(n):
            nonlocal top
            l = tk.Label(top, text='Test{}'.format(n))
            l.config(cursor='hand', relief='ridge')
            l.bind('<Enter>', enter_leave)
            l.bind('<Leave>', enter_leave)
            l.bind('<Button-1>', lambda _: top.destroy())
            l.grid(row=n, column=0)

        def enter_leave(e):
            # 7 = enter
            # 8 = leave
            if e.type == '7':
                e.widget.config(bg='grey')
            else:
                e.widget.config(bg='white')

        # populate some labels
        for x in range(9):
            create_label(x)

        self.update_idletasks()

        top_width = top.winfo_width()
        top_height = top.winfo_height()

        root_x = self.winfo_x()
        root_y = self.winfo_y()

        top.geometry('{}x{}+{}+{}'.format(
            top_width, top_height,
            int(root_x + (self.winfo_width() / 2)),
            root_y + 50
        ))

if __name__ == '__main__':
    App()

总而言之,我只想让“突出显示”效果发挥作用,但图像1的外观没有标题栏。我愿意接受所有其他建议或方法。

解决问题了吗?它看起来在ubuntu上运行良好(除了手动光标>必须是hand1)@PRMoureu不幸的是,没有。该解决方案适用于整个应用程序,与其他开放程序相比没有焦点。我的问题是应用程序本身的焦点。在ubuntu上工作很有趣。是的,这是一个窗口管理器的区别。删除窗口中的边框装饰(OverrideDirect)时,在传播事件时,窗口管理器将完全忽略窗口。这在Tk手册中有所记录,但它并没有告诉您进入/离开事件被忽略,而是被忽略。
top.attributes('-topmost', True)
top.lift(aboveThis=self)
top.focus_force()
top.grab_set()