PyGTK 中简单、通用且可重用的输入对话框(有时称为输入对话框)

发布于 2024-12-18 13:06:56 字数 149 浏览 0 评论 0原文

我正在寻找一个带有文本输入小部件的简单对话框,要求用户提供一些输入。该对话框应该易于运行(如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坐在坟头思考人生 2024-12-25 13:06:56

基于我发现的示例(感谢Ardoris!),我想出了一个对话框子类...希望它可以帮助那里的人!

#!/usr/bin/env python
import gtk
class EntryDialog(gtk.MessageDialog):
    def __init__(self, *args, **kwargs):
        '''
        Creates a new EntryDialog. Takes all the arguments of the usual
        MessageDialog constructor plus one optional named argument 
        "default_value" to specify the initial contents of the entry.
        '''
        if 'default_value' in kwargs:
            default_value = kwargs['default_value']
            del kwargs['default_value']
        else:
            default_value = ''
        super(EntryDialog, self).__init__(*args, **kwargs)
        entry = gtk.Entry()        
        entry.set_text(str(default_value))
        entry.connect("activate", 
                      lambda ent, dlg, resp: dlg.response(resp), 
                      self, gtk.RESPONSE_OK)
        self.vbox.pack_end(entry, True, True, 0)
        self.vbox.show_all()
        self.entry = entry
    def set_value(self, text):
        self.entry.set_text(text)
    def run(self):
        result = super(EntryDialog, self).run()
        if result == gtk.RESPONSE_OK:
            text = self.entry.get_text()
        else:
            text = None
        return text

如果用户按 或单击“确定”,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!

#!/usr/bin/env python
import gtk
class EntryDialog(gtk.MessageDialog):
    def __init__(self, *args, **kwargs):
        '''
        Creates a new EntryDialog. Takes all the arguments of the usual
        MessageDialog constructor plus one optional named argument 
        "default_value" to specify the initial contents of the entry.
        '''
        if 'default_value' in kwargs:
            default_value = kwargs['default_value']
            del kwargs['default_value']
        else:
            default_value = ''
        super(EntryDialog, self).__init__(*args, **kwargs)
        entry = gtk.Entry()        
        entry.set_text(str(default_value))
        entry.connect("activate", 
                      lambda ent, dlg, resp: dlg.response(resp), 
                      self, gtk.RESPONSE_OK)
        self.vbox.pack_end(entry, True, True, 0)
        self.vbox.show_all()
        self.entry = entry
    def set_value(self, text):
        self.entry.set_text(text)
    def run(self):
        result = super(EntryDialog, self).run()
        if result == gtk.RESPONSE_OK:
            text = self.entry.get_text()
        else:
            text = None
        return text

The run() method returns either the text entered in the entry box if the user presses <Enter> or clicks Ok. If Cancel is clicked or <Esc> pressed, the run() method returns None.

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.

笑咖 2024-12-25 13:06:56

GTK+ 中没有可用的。您有两个选择:

  • 创建一个对话框,打包 Entry 和您需要的任何其他内容(在我看来可能是最好的方法)
  • 检索 MessageDialog 的 content_area 并向其附加一个 Entry。

大致如下:

#!/usr/bin/env python

import gtk

messagedialog = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_OK, message_format="Hello")

action_area = messagedialog.get_content_area()

entry = gtk.Entry()
action_area.pack_start(entry)

messagedialog.show_all()
messagedialog.run()
messagedialog.destroy()

尽管它可能需要更多的改进才能让条目更好地显示。

There isn't one available in GTK+. You've got two options:

  • Create a dialog, pack the Entry and any other content you need (probably the best way in my opinion)
  • Retrieve the content_area of the MessageDialog and append an Entry to it.

Something along the lines of:

#!/usr/bin/env python

import gtk

messagedialog = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_OK, message_format="Hello")

action_area = messagedialog.get_content_area()

entry = gtk.Entry()
action_area.pack_start(entry)

messagedialog.show_all()
messagedialog.run()
messagedialog.destroy()

Though it does probably need more refinement to get the Entry to display nicely.

染墨丶若流云 2024-12-25 13:06:56

这是我根据之前的答案编写的函数。它是一个函数而不是一个类,这意味着您可以在一行中使用它。

def get_text(parent, message, default=''):
    """
    Display a dialog with a text entry.
    Returns the text, or None if canceled.
    """
    d = gtk.MessageDialog(parent,
                          gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                          gtk.MESSAGE_QUESTION,
                          gtk.BUTTONS_OK_CANCEL,
                          message)
    entry = gtk.Entry()
    entry.set_text(default)
    entry.show()
    d.vbox.pack_end(entry)
    entry.connect('activate', lambda _: d.response(gtk.RESPONSE_OK))
    d.set_default_response(gtk.RESPONSE_OK)

    r = d.run()
    text = entry.get_text().decode('utf8')
    d.destroy()
    if r == gtk.RESPONSE_OK:
        return text
    else:
        return None

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.

def get_text(parent, message, default=''):
    """
    Display a dialog with a text entry.
    Returns the text, or None if canceled.
    """
    d = gtk.MessageDialog(parent,
                          gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                          gtk.MESSAGE_QUESTION,
                          gtk.BUTTONS_OK_CANCEL,
                          message)
    entry = gtk.Entry()
    entry.set_text(default)
    entry.show()
    d.vbox.pack_end(entry)
    entry.connect('activate', lambda _: d.response(gtk.RESPONSE_OK))
    d.set_default_response(gtk.RESPONSE_OK)

    r = d.run()
    text = entry.get_text().decode('utf8')
    d.destroy()
    if r == gtk.RESPONSE_OK:
        return text
    else:
        return None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文