PyGTK 中简单、通用且可重用的输入对话框(有时称为输入对话框)
我正在寻找一个带有文本输入小部件的简单对话框,要求用户提供一些输入。该对话框应该易于运行(如 gtk.MessageDialog 变体)并且灵活。
当然有一些例子,但它们要么不够灵活,要么太复杂,无法按照我的口味构建。
我讨厌重新发明轮子……或者对话。
I am searching for a simple dialog with a text entry widget asking the user for some input. The dialog should be easy to run (like the gtk.MessageDialog
variants) and as flexible.
There are of course some examples but they are either not flexible enough or too complicated to construct for my taste.
I hate re-inventing the wheel... or a dialog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于我发现的示例(感谢Ardoris!),我想出了一个对话框子类...希望它可以帮助那里的人!
如果用户按
或单击“确定”,run()
方法将返回在输入框中输入的文本。如果单击Cancel
或按下
,run()
方法将返回None
。除此之外,对话框的行为应与任何其他 gtk.MessageDialog 实例相同。
也许这不是非常通用,因为它假设您始终将 Ok 作为一个选项,但无论如何,这就是我在 99% 的用例中所需要的。
Based on an example I found (thanks Ardoris!), I came up with a dialog subclass... hope it helps someone out there!
The
run()
method returns either the text entered in the entry box if the user presses<Enter>
or clicks Ok. IfCancel
is clicked or<Esc>
pressed, therun()
method returnsNone
.Except for that, the dialog should behave as any other
gtk.MessageDialog
instance.Maybe that is not very general as it assumes you will always have Ok as an option, but that is what I need in 99% of my use cases anyway.
GTK+ 中没有可用的。您有两个选择:
大致如下:
尽管它可能需要更多的改进才能让条目更好地显示。
There isn't one available in GTK+. You've got two options:
Something along the lines of:
Though it does probably need more refinement to get the Entry to display nicely.
这是我根据之前的答案编写的函数。它是一个函数而不是一个类,这意味着您可以在一行中使用它。
Here's the function I wrote, based on the previous answers here. It's a function instead of a class, which means you can use it in one line.