0

我正在制作一个自动生成的 Tkinter GUI(如 HTML 表单),并且我正在尝试制作一个类似于 HTML 使用的文件名字段。这是我的代码:

e = ttk.Entry(master)
e.grid(row=ROW, column=1)
b = ttk.Button(master, text="...")
b.grid(row=ROW, column=2, sticky=tkinter.E)

我希望这样当用户单击...按钮时,会弹出一个文件名对话框(我知道如何执行该部分),并且当用户选择一个文件时,它会反映在条目中(这是我拥有的部分麻烦了)。我在那部分遇到麻烦的原因是 E 和 B 不断变化,因为这是循环运行的,所以我认为使这项工作的唯一方法是使用行检测器,并以某种方式更改 Entry 框的值. 我该怎么做?

提前致谢!如果这有任何不清楚的地方,请告诉我。

4

1 回答 1

1

最好的解决方案之一是将您自己的小部件创建为一个类。像这样的东西:

class MyWidget(self, tkinter.Frame):
    def __init__(self, *args, **kwargs):
        tkinter.Frame.__init__(self, *args, **kwargs)
        self.entry = tkinter.Entry(self)
        self.button = tkinter.Button(self, text="...",
                                     command=self._on_button)
        self.button.pack(side="right")
        self.entry.poack(side="left", fill="both", expand=True)

    def _on_button(self):
        s = <call whatever dialog you want...>
        if s != "":
            self.entry.delete(0, "end")
            self.entry.insert(0, s)

...
entry = MyWidget(master)
entry.grid(...)
...
于 2012-06-22T02:17:50.973 回答