我的计划有什么问题?它工作正常,直到它试图显示对话框然后它是段错误。
请原谅我,如果解决方案很明显,这只是我的第二个GTK +计划。
import time, threading
from sys import exit
from gi.repository import Gtk, GObject
def run_core_engine():
time.sleep(2)
window.switchToMainFrame()
time.sleep(2.5)
window.failDialog("The window said hello")
class CoreThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True
super(CoreThread, self).start()
def run(self):
run_core_engine()
class GettingInfoFrame(Gtk.Frame):
def __init__(self):
Gtk.Frame.__init__(self)
self.connect("delete-event", Gtk.main_quit)
self.set_shadow_type(Gtk.ShadowType.NONE)
self.set_border_width(45)
vbox = Gtk.VBox(spacing=10)
hbox = Gtk.HBox(spacing=10)
text = Gtk.Label()
text.set_markup("<b><big>Doing stuff...</big></b>")
self.spinner = Gtk.Spinner()
self.spinner.set_size_request(30, 30)
self.spinner.start()
hbox.pack_start(text, True, True, 10)
hbox.pack_start(self.spinner, True, True, 10)
vbox.pack_start(hbox, True, True, 10)
self.add(vbox)
class TheMainFrame(Gtk.Frame):
def __init__(self):
Gtk.Frame.__init__(self)
self.set_shadow_type(Gtk.ShadowType.NONE)
self.set_border_width(150)
label = Gtk.Label("Hello")
box = Gtk.VBox()
box.pack_start(label, True, True, 0)
self.add(box)
class Window(Gtk.Window):
def __init__(self):
## Setup Main Window
Gtk.Window.__init__(self, title="Fred the window")
self.connect("delete-event", Gtk.main_quit)
self.set_resizable(False)
self.gettingInfoFrame = GettingInfoFrame()
self.add(self.gettingInfoFrame)
def switchToMainFrame(self):
print("Displaying information in window")
self.theMainFrame = TheMainFrame()
self.remove(self.get_child())
self.add(self.theMainFrame)
self.show_all()
def failDialog(self, message):
dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.INFO,
Gtk.ButtonsType.OK, "INFO:")
dialog.format_secondary_text(message)
dialog.run() **# <-- Where the error occurs**
Gtk.main_quit()
GObject.threads_init()
core_thread = CoreThread()
window = Window()
window.show_all()
Gtk.main()
错误行话:
在窗口中显示信息
(2ndgtk + .py:32073):Gtk-WARNING **:GtkMessageDialog 0x7ff51400a040: widget试图在GtkWidget :: get_width中使用gtk_widget_get_width 实现。应该只是调用 GTK_WIDGET_GET_CLASS(小部件) - &gt; get_width直接而不是使用 gtk_widget_get_width
(2ndgtk + .py:32073):Gtk-WARNING **:GtkBox 0x7ff514004080:widget 尝试在GtkWidget :: get_width中使用gtk_widget_get_width 实现。应该只是调用 GTK_WIDGET_GET_CLASS(小部件) - &gt; get_width直接而不是使用 gtk_widget_get_width
(2ndgtk + .py:32073):Gtk-WARNING **:GtkButtonBox 0x2170620:widget 尝试在GtkWidget :: get_width中使用gtk_widget_get_width 实现。应该只是调用 GTK_WIDGET_GET_CLASS(小部件) - &gt; get_width直接而不是使用 gtk_widget_get_width
(2ndgtk + .py:32073):Gtk-WARNING **:GtkButton 0x7ff514010050:widget 试图在GtkWidget :: get_height中使用gtk_widget_get_height 实现。应该只是调用 GTK_WIDGET_GET_CLASS(小部件) - &gt; get_height直接而不是使用 gtk_widget_get_height
(2ndgtk + .py:32073):Gtk-WARNING **:GtkAlignment 0x7ff514014070: 小部件试图在GtkWidget中使用gtk_widget_get_height :: get_height实现。应该只是调用 GTK_WIDGET_GET_CLASS(小部件) - &gt; get_height直接而不是使用 gtk_widget_get_height
(2ndgtk + .py:32073):Gtk-WARNING **:GtkBox 0x7ff514004320:widget 试图在GtkWidget :: get_height中使用gtk_widget_get_height 实现。应该只是调用 GTK_WIDGET_GET_CLASS(小部件) - &gt; get_height直接而不是使用 gtk_widget_get_height
(2ndgtk + .py:32073):Gtk-WARNING **:GtkLabel 0x22a2ac0:小部件尝试过 到GtkWidget :: get_height里面的gtk_widget_get_height 实现。应该只是调用 GTK_WIDGET_GET_CLASS(小部件) - &gt; get_height直接而不是使用 gtk_widget_get_height分段错误
答案 0 :(得分:1)
我明白了。问题是直接调用“window.failDialog”而不是GLib.idle_add。 我添加了一些其他细微的变化。
这是新代码:
import time, threading
from sys import exit
from gi.repository import Gtk, GObject, GLib
def run_core_engine():
time.sleep(2)
window.switchToMainFrame()
time.sleep(2.5)
GLib.idle_add(window.failDialog, "The window said hello")
class CoreThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True
super(CoreThread, self).start()
def run(self):
run_core_engine()
class GettingInfoFrame(Gtk.Frame):
def __init__(self):
Gtk.Frame.__init__(self)
self.connect("delete-event", Gtk.main_quit)
self.set_shadow_type(Gtk.ShadowType.NONE)
self.set_border_width(45)
vbox = Gtk.VBox(spacing=10)
hbox = Gtk.HBox(spacing=10)
text = Gtk.Label()
text.set_markup("<b><big>Doing stuff...</big></b>")
self.spinner = Gtk.Spinner()
self.spinner.set_size_request(30, 30)
self.spinner.start()
hbox.pack_start(text, True, True, 10)
hbox.pack_start(self.spinner, True, True, 10)
vbox.pack_start(hbox, True, True, 10)
self.add(vbox)
class TheMainFrame(Gtk.Frame):
def __init__(self):
Gtk.Frame.__init__(self)
self.set_shadow_type(Gtk.ShadowType.NONE)
self.set_border_width(150)
label = Gtk.Label("Hello")
box = Gtk.VBox()
box.pack_start(label, True, True, 0)
self.add(box)
class Window(Gtk.Window):
def __init__(self):
## Setup Main Window
Gtk.Window.__init__(self, title="Fred the window")
self.connect("delete-event", Gtk.main_quit)
self.set_resizable(False)
self.gettingInfoFrame = GettingInfoFrame()
self.add(self.gettingInfoFrame)
def switchToMainFrame(self):
print("Displaying information in window")
self.theMainFrame = TheMainFrame()
self.remove(self.get_child())
self.add(self.theMainFrame)
self.show_all()
def failDialog(self, errorMessage):
dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.INFO,
Gtk.ButtonsType.OK, "INFO:")
dialog.format_secondary_text(errorMessage)
dialog.run()
Gtk.main_quit()
GObject.threads_init()
core_thread = CoreThread()
window = Window()
window.show_all()
Gtk.main()