我正在使用PyGObject和GTK + 3处理一个简单的GUI应用程序。在这种情况下,我想要一个按钮,弹出一个对话框,当你单击OK时会添加一个项目到一个列表。我有那部分工作,但最后一部分不起作用是将项目添加到列表中。看来项目确实已添加,但它已空。它可以选择,但非常小。我尝试添加其他类型的小部件,如Gtk.Button,看看它是否与Gtk.Label有些奇怪。当我在构造函数中添加Gtk.Label时,它可以正常工作。
此外,我知道这不是一个很好的做事方式,我在代码中做的事情有些奇怪但我还在学习如何使用PyGObject / GTK +我想这个问题只是我忽视的愚蠢行为。
MainWindow.py
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import PromptDialog
class MainWindow(Gtk.Window):
def addURLResponse(self, dialog, response, listBox):
if(response == Gtk.ResponseType.OK):
print(dialog.get_text())
label = Gtk.Label(dialog.get_text())
print(label.get_text())
listBox.add(label)
if(response != Gtk.ResponseType.DELETE_EVENT):
dialog.destroy()
def addURL(self, button):
URLDialog = PromptDialog.PromptDialog("Add URL", self)
URLDialog.connect('response', self.addURLResponse, button.get_parent().get_parent().get_parent().get_children()[1])
URLDialog.show_all()
def __init__(self):
Gtk.Window.__init__(self, title="MPV-VJ")
self.playlistsBar = Gtk.FlowBox()
self.newBtn = Gtk.Button.new_with_label('+')
self.playlistsBar.add(self.newBtn)
self.playlistsList = Gtk.ListBox()
self.playlistsView = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.playlistsView.pack_start(self.playlistsBar, False, False, 0)
self.playlistsView.pack_start(self.playlistsList, True, True, 0)
self.playlist1View = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.playlist1Bar = Gtk.FlowBox()
self.addUrl1Btn = Gtk.Button.new_with_label('+URL')
self.addUrl1Btn.connect('clicked', self.addURL)
self.playlist1Bar.add(self.addUrl1Btn)
self.addFile1Btn = Gtk.Button.new_with_label('+file')
self.playlist1Bar.add(self.addFile1Btn)
self.addDir1Btn = Gtk.Button.new_with_label('+dir')
self.playlist1Bar.add(self.addDir1Btn)
self.playlist1List = Gtk.ListBox()
self.playlist1View.pack_start(self.playlist1Bar, False, False, 0)
self.playlist1View.pack_start(self.playlist1List, True, True, 0)
self.playlist2View = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.playlist2Bar = Gtk.FlowBox()
self.addUrl2Btn = Gtk.Button.new_with_label('+URL')
self.playlist2Bar.add(self.addUrl2Btn)
self.addFile2Btn = Gtk.Button.new_with_label('+file')
self.playlist2Bar.add(self.addFile2Btn)
self.addDir2Btn = Gtk.Button.new_with_label('+dir')
self.playlist2Bar.add(self.addDir2Btn)
self.playlist2List = Gtk.ListBox()
self.playlist2View.pack_start(self.playlist2Bar, False, False, 0)
self.playlist2View.pack_start(self.playlist2List, True, True, 0)
self.plViewsBox = Gtk.HPaned()
self.plViewsBox.pack1(self.playlist1View, True, False)
self.plViewsBox.pack2(self.playlist2View, True, False)
self.viewBox = Gtk.HPaned()
self.viewBox.pack1(self.playlistsView, True, False)
self.viewBox.pack2(self.plViewsBox, True, False)
self.viewBox.set_position(200)
self.logView = Gtk.ListBox()
self.contentBox = Gtk.VPaned()
self.contentBox.pack1(self.viewBox, True, False)
self.contentBox.pack2(self.logView, True, False)
self.contentBox.set_position(400)
self.toolBar = Gtk.FlowBox()
self.newBtn = Gtk.Button.new_with_label('new')
self.toolBar.add(self.newBtn)
self.loadBtn = Gtk.Button.new_with_label('load')
self.toolBar.add(self.loadBtn)
self.saveBtn = Gtk.Button.new_with_label('save')
self.toolBar.add(self.saveBtn)
self.mainBox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.mainBox.pack_start(self.toolBar, False, False, 0)
self.mainBox.pack_start(self.contentBox, True, True, 0)
self.add(self.mainBox)
self.resize(1000, 500)
PromptDialog.py
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class PromptDialog(Gtk.Dialog):
def get_text(self):
return(self.entry.get_buffer().get_text())
def __init__(self, message, mainWindow):
Gtk.Dialog.__init__(self, title="Prompt")
self.set_modal(True)
self.set_transient_for(mainWindow)
self.label = Gtk.Label(message)
self.entry = Gtk.Entry()
self.get_content_area().pack_start(self.label, True, True, 0)
self.get_content_area().pack_start(self.entry, True, True, 0)
self.add_button("OK", Gtk.ResponseType.OK)
self.add_button("Cancel", Gtk.ResponseType.CANCEL)
答案 0 :(得分:0)
添加到窗口的窗口小部件需要在它出现之前调用其.show()方法。