python-3.x - 如何允许在 Gtk.FileChooserDialog 中单击鼠标?
问题描述
Gtk.FileChooserDialog
下面是我改编自Gtk 文档的小部件的简化示例代码。要选择文件或文件夹或激活此小部件中的任何内容,目前我必须将鼠标指针放在项目上并双击它。相反,我希望使用鼠标单击来进行选择和激活。如何为此小部件进行设置?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class FileChooserWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="FileChooser Example")
box = Gtk.Box(spacing=6)
self.add(box)
button1 = Gtk.Button("Choose File")
button1.connect("clicked", self.on_file_clicked)
box.add(button1)
def on_file_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a file", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
win = FileChooserWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
解决方案
您可以像这样使用"selection-changed"
信号:
def selection_changed (filechooser, udata):
print ("selected ", filechooser.get_filename()) # GtkFileChooser method
if True: # some selection checking
filechooser.response(Gtk.ResponseType.OK) # GtkDialog method
dialog = Gtk.FileChooserDialog(...)
dialog.connect ("selection-changed", selection_changed, None)
response = dialog.run()
if response == Gtk.ResponseType.OK:
pass
elif response == Gtk.ResponseType.CANCEL:
pass
dialog.destroy()
但是每次用户与文件选择器交互时都会发出此信号,即使用户使用面包屑按钮更改文件夹也是如此。由您决定是否该回复“确定”、“取消”或根本不回复。
此外,用户可能会感到困惑,需要按下的对话框Ok
或Enter
实际上在所有其他应用程序中的行为在您的应用程序中表现不同。
推荐阅读
- regex - 通过 sed 取消注释关闭荣誉
- node.js - Angular Universal - TypeError: Object(...) is not a function at new ApplicationRef
- python - 用函数打破while True循环
- r - 提取多个子字符串并为每个子字符串返回一个值
- java - 循环后变量被重置
- liferay-6 - 原因:java.lang.IllegalStateException:无法获取 Writer,因为 OutputStream 已在使用中
- mysql - 在多个表上查询具有不同值的相同表Mysql
- symfony - 如何在 symfony 中,在第二个数据库中创建新实体
- apache-spark - 火花蓄能器,我总是得到 0 值
- android - 在 Android 上接收(和发送)USSD 消息 - 后台应用程序,屏幕锁定