gpt4 book ai didi

Python GTK。如何连续显示对话框?

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

我想在带有列表框的自定义对话框上显示对话框。如果列表框中的选择错误,则显示消息并允许再次选择正确的答案。我编写了这个演示代码来演示该问题:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from ListBoxRowWithData import ListBoxRowWithData
import shutil
from InterfaceChooserDialog import InterfaceChooserDialog
import netifaces as ni

class InterfaceChooserDialog(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "Choose interface", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.connect("response", self.on_response)

wrapper_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
description_label = Gtk.Label("Select one")
wrapper_box.pack_start(description_label, False, False, 0)
self.listbox = Gtk.ListBox()
strings = ["12", "22", "32"]

for single_string in strings:
row = Gtk.ListBoxRow()
row.string = single_string
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
label = Gtk.Label(single_string, xalign=0)
hbox.pack_start(label, True, True, 0)
self.listbox.add(row)

self.selected_interface = self.listbox.get_row_at_index(0)
self.listbox.select_row(self.selected_interface)
wrapper_box.pack_start(self.listbox, False, False, 0)
box = self.get_content_area()
box.add(wrapper_box)
self.show_all()

def on_response(self, widget, response_id):
self.selected_one = self.listbox.get_selected_row()

def get_selected_string(self):
return self.selected_one.string

class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Example")
self.set_default_size(600, 480)
dialog = InterfaceChooserDialog(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
selected_string = dialog.get_selected_string()
print("Selected string", selected_string)
if selected_string == 12:
print("selected")
dialog.destroy()
else:
dialog_wrong = Gtk.MessageDialog(self, 0, Gtk.MessageType.INFO,
Gtk.ButtonsType.OK, "This is wrong. Select 12")
dialog_wrong.run()
dialog_wrong.destroy()


elif response == Gtk.ResponseType.CANCEL:
dialog.destroy()


win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()

但它不允许显示自定义对话框多次显示。我该如何解决这个问题?

最佳答案

您需要在最后运行Gtk.main(),以便主循环开始运行并且程序在对话框显示后不会退出。

如果您这样做,您可能还想在构造函数中运行 win.show(),以便窗口不会出现在对话框之后。

关于Python GTK。如何连续显示对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55059396/

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