gpt4 book ai didi

gtk3 - Python/Gtk3 : How to add a Gtk. 进入 Gtk.MessageDialog?

转载 作者:行者123 更新时间:2023-12-05 01:01:24 25 4
gpt4 key购买 nike

早上好,

我正在尝试添加 Gtk.EntryGtk.MessageDialog .使用以下代码,我似乎添加了 Gtk.Entry但它在对话窗口中不可见 (Python3/Gtk3) :

#!/usr/bin/python3

from gi.repository import Gtk

def get_user_pw(parent, message, default=''):
dialogWindow = Gtk.MessageDialog(parent,
Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.QUESTION,
Gtk.ButtonsType.OK_CANCEL,
message)

dialogBox = dialogWindow.get_content_area()
userEntry = Gtk.Entry()
userEntry.set_visibility(False)
userEntry.set_invisible_char("*")
userEntry.set_size_request(250,0)
userEntry.set_text("Test")
dialogBox.pack_end(userEntry, False, False, 0)
#dialogWindow.vbox.pack_start(userEntry, False, False, 0)

response = dialogWindow.run()
text = userEntry.get_text()
dialogWindow.destroy()
if response == Gtk.ResponseType.OK:
return text
else:
return None

class MainWindow(Gtk.Window):

def __init__(self):

Gtk.Window.__init__(self, title="MyWindowTitle")

userPassphrase = get_user_pw(self, "SSH key passphrase")
print("User passphrase: " + userPassphrase)

此代码打印:
User passphrase: Test

我正在寻找有关使条目可见和可编辑的线索,欢迎任何帮助。

引用:
http://python-gtk-3-tutorial.readthedocs.org/en/latest/dialogs.html
http://developer.gnome.org/gtk3/3.2/GtkDialog.html
Simple, versatile and re-usable entry dialog (sometimes referred to as input dialog) in PyGTK

最佳答案

好的,现在可以使用了,我需要 show_all()之前 run() .我花了一些时间才弄明白这个简单的事情。调试代码是:

def get_user_pw(parent, message, title=''):
# Returns user input as a string or None
# If user does not input text it returns None, NOT AN EMPTY STRING.
dialogWindow = Gtk.MessageDialog(parent,
Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.QUESTION,
Gtk.ButtonsType.OK_CANCEL,
message)

dialogWindow.set_title(title)

dialogBox = dialogWindow.get_content_area()
userEntry = Gtk.Entry()
userEntry.set_visibility(False)
userEntry.set_invisible_char("*")
userEntry.set_size_request(250,0)
dialogBox.pack_end(userEntry, False, False, 0)

dialogWindow.show_all()
response = dialogWindow.run()
text = userEntry.get_text()
dialogWindow.destroy()
if (response == Gtk.ResponseType.OK) and (text != ''):
return text
else:
return None

我像这样使用它:
class MainWindow(Gtk.Window):

def __init__(self):
Gtk.Window.__init__(self, title="MyWindowTitle")
userPassword = get_user_pw(self, "Please enter your password", "Password")

关于gtk3 - Python/Gtk3 : How to add a Gtk. 进入 Gtk.MessageDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13970445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com