The ::close signal is a keybinding signal which gets emitted when the user uses a keybinding to close the dialog.
response
Emitted when an action widget is clicked, the dialog receives a delete event, or the application programmer calls Gtk.Dialog.response().
Gtk.MessageDialog
继承关系
Methods
方法修饰词
方法名及参数
format_secondary_markup (message_format)
format_secondary_text (message_format)
get_image ()
get_message_area ()
set_image (image)
set_markup (str)
Properties
Name
Type
Flags
Short Description
buttons
Gtk.ButtonsType
w/co
The buttons shown in the message dialog
image
Gtk.Widget
d/r/w
The image deprecated
message-area
Gtk.Widget
r
Gtk.VBox that holds the dialog’s primary and secondary labels
message-type
Gtk.MessageType
r/w/c/en
The type of message
secondary-text
str
r/w
The secondary text of the message dialog
secondary-use-markup
bool
r/w/en
The secondary text includes Pango markup.
text
str
r/w
The primary text of the message dialog
use-markup
bool
r/w/en
The primary text of the title includes Pango markup.
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/16# section 045
TITLE = "MessageDialog"
DESCRIPTION = """
Gtk.MessageDialog presents a dialog with some message text.
It’s simply a convenience widget; you could construct the equivalent of Gtk.MessageDialog
from Gtk.Dialog without too much effort, but Gtk.MessageDialog saves typing.
"""import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
classMessageDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="MessageDialog Example")
box = Gtk.Box(spacing=6)
self.add(box)
button1 = Gtk.Button("Information")
button1.connect("clicked", self.on_info_clicked)
box.add(button1)
button2 = Gtk.Button("Error")
button2.connect("clicked", self.on_error_clicked)
box.add(button2)
button3 = Gtk.Button("Warning")
button3.connect("clicked", self.on_warn_clicked)
box.add(button3)
button4 = Gtk.Button("Question")
button4.connect("clicked", self.on_question_clicked)
box.add(button4)
defon_info_clicked(self, widget):
dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.INFO,
Gtk.ButtonsType.OK, "This is an INFO MessageDialog")
dialog.format_secondary_text(
"And this is the secondary text that explains things.")
dialog.run()
print("INFO dialog closed")
dialog.destroy()
defon_error_clicked(self, widget):
dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR,
Gtk.ButtonsType.CANCEL, "This is an ERROR MessageDialog")
dialog.format_secondary_text(
"And this is the secondary text that explains things.")
dialog.run()
print("ERROR dialog closed")
dialog.destroy()
defon_warn_clicked(self, widget):
dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.WARNING,
Gtk.ButtonsType.OK_CANCEL, "This is an WARNING MessageDialog")
dialog.format_secondary_text(
"And this is the secondary text that explains things.")
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("WARN dialog closed by clicking OK button")
elif response == Gtk.ResponseType.CANCEL:
print("WARN dialog closed by clicking CANCEL button")
dialog.destroy()
defon_question_clicked(self, widget):
dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.QUESTION,
Gtk.ButtonsType.YES_NO, "This is an QUESTION MessageDialog")
dialog.format_secondary_text(
"And this is the secondary text that explains things.")
response = dialog.run()
if response == Gtk.ResponseType.YES:
print("QUESTION dialog closed by clicking YES button")
elif response == Gtk.ResponseType.NO:
print("QUESTION dialog closed by clicking NO button")
dialog.destroy()
defmain():
win = MessageDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Gtk.ColorChooserDialog
继承关系
Methods
方法修饰词
方法名及参数
static
new (title, parent)
Properties
Name
Type
Flags
Short Description
show-editor
bool
r/w
Show editor
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/27# section 046
TITLE = "ColorChooserDialog"
DESCRIPTION = """
The Gtk.ColorChooserDialog widget is a dialog for choosing a color.
It implements the Gtk.ColorChooser interface.
"""import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Pango
classColorChooserDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="ColorChooserDialog Demo")
self.set_border_width(10)
self.set_default_size(200, 100)
self.textview = Gtk.TextView()
self.textview.set_editable(False)
self.buffer = self.textview.get_buffer()
self.buffer.set_text("The only victory over love is flight.", -1)
button = Gtk.Button("Select Color")
button.connect("clicked", self.on_clicked)
fix = Gtk.Fixed()
fix.put(button, 50, 30)
fix.put(self.textview, 30, 90)
self.add(fix)
self.show_all()
defon_clicked(self, widget):
cdia = Gtk.ColorChooserDialog()
response = cdia.run()
if response == Gtk.ResponseType.OK:
rgba = cdia.get_rgba()
# 改变背景颜色# self.textview.modify_bg(Gtk.StateType.NORMAL, rgba.to_color())# 改变文字颜色
self.textview.override_color(Gtk.StateFlags.NORMAL, rgba)
cdia.destroy()
defmain():
win = ColorChooserDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Gtk.ColorSelectionDialog
继承关系
Methods
方法修饰词
方法名及参数
static
new (title)
get_color_selection ()
Properties
Name
Type
Flags
Short Description
cancel-button
Gtk.Widget
r
The cancel button of the dialog.
color-selection
Gtk.Widget
r
The color selection embedded in the dialog.
help-button
Gtk.Widget
r
The help button of the dialog.
ok-button
Gtk.Widget
r
The OK button of the dialog.
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/27# section 047
TITLE = "ColorSelectionDialog"
DESCRIPTION = ""import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Pango
classColorSelectionDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="ColorSelectionDialog Demo")
self.set_border_width(10)
self.set_default_size(200, 100)
self.textview = Gtk.TextView()
self.textview.set_editable(False)
self.buffer = self.textview.get_buffer()
self.buffer.set_text("The only victory over love is flight.", -1)
button = Gtk.Button("Select Color")
button.connect("clicked", self.on_clicked)
fix = Gtk.Fixed()
fix.put(button, 50, 30)
fix.put(self.textview, 30, 90)
self.add(fix)
self.show_all()
defon_clicked(self, widget):
cdia = Gtk.ColorSelectionDialog()
response = cdia.run()
if response == Gtk.ResponseType.OK:
# widget Gtk.ColorSelection
colorselection = cdia.get_color_selection()
rgba = colorselection.get_current_rgba()
# 改变背景颜色# self.textview.modify_bg(Gtk.StateType.NORMAL, rgba.to_color())# 改变文字颜色
self.textview.override_color(Gtk.StateFlags.NORMAL, rgba)
cdia.destroy()
defmain(demoapp=None):
win = ColorSelectionDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Gtk.FontChooserDialog
继承关系
Methods
方法修饰词
方法名及参数
static
new (title, parent)
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/27# section 048
TITLE = "FontChooseDialog"
DESCRIPTION = """
The Gtk.FontChooserDialog widget is a dialog for selecting a font.
It implements the Gtk.FontChooser interface.
"""import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Pango
classFontChooserDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="FontChooserDialog Demo")
self.set_border_width(10)
self.set_default_size(200, 100)
self.label = Gtk.Label("The only victory over love is flight.")
button = Gtk.Button("Select font")
button.connect("clicked", self.on_clicked)
fix = Gtk.Fixed()
fix.put(button, 50, 30)
fix.put(self.label, 30, 90)
self.add(fix)
self.show_all()
defon_clicked(self, widget):
fdia = Gtk.FontChooserDialog("Select font name")
response = fdia.run()
if response == Gtk.ResponseType.OK:
font_desc = fdia.get_font_desc()
if font_desc:
self.label.modify_font(font_desc)
fdia.destroy()
defmain():
win = FontChooserDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Gtk.FontSelectionDialog
继承关系
Methods
方法修饰词
方法名及参数
static
new (title)
get_cancel_button ()
get_font_name ()
get_font_selection ()
get_ok_button ()
get_preview_text ()
set_font_name (fontname)
set_preview_text (text)
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/27# section 049
TITLE = "FontSelectionDialog"
DESCRIPTION = ""import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Pango
classFontChooseDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="FontSelectionDialog Demo")
self.set_border_width(10)
self.set_default_size(200, 100)
self.label = Gtk.Label("The only victory over love is flight.")
button = Gtk.Button("Select font")
button.connect("clicked", self.on_clicked)
fix = Gtk.Fixed()
fix.put(button, 50, 30)
fix.put(self.label, 30, 90)
self.add(fix)
self.show_all()
defon_clicked(self, widget):
fdia = Gtk.FontSelectionDialog("Select font name")
response = fdia.run()
if response == Gtk.ResponseType.OK:
font_desc = Pango.FontDescription(fdia.get_font_name())
if font_desc:
self.label.modify_font(font_desc)
fdia.destroy()
defmain():
win = FontChooseDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Gtk.AboutDialog
继承关系
Methods
方法修饰词
方法名及参数
static
new ()
add_credit_section (section_name, people)
get_artists ()
get_authors ()
get_comments ()
get_copyright ()
get_documenters ()
get_license ()
get_license_type ()
get_logo ()
get_logo_icon_name ()
get_program_name ()
get_translator_credits ()
get_version ()
get_website ()
get_website_label ()
get_wrap_license ()
set_artists (artists)
set_authors (authors)
set_comments (comments)
set_copyright (copyright)
set_documenters (documenters)
set_license (license)
set_license_type (license_type)
set_logo (logo)
set_logo_icon_name (icon_name)
set_program_name (name)
set_translator_credits (translator_credits)
set_version (version)
set_website (website)
set_website_label (website_label)
set_wrap_license (wrap_license)
Virtual Methods
do_activate_link (uri)
Properties
Name
Type
Flags
Short Description
artists
[str]
r/w/en
List of people who have contributed artwork to the program
authors
[str]
r/w/en
List of authors of the program
comments
str
r/w/en
Comments about the program
copyright
str
r/w/en
Copyright information for the program
documenters
[str]
r/w/en
List of people documenting the program
license
str
r/w/en
The license of the program
license-type
Gtk.License
r/w/en
The license type of the program
logo
GdkPixbuf.Pixbuf
r/w/en
A logo for the about box. If this is not set, it defaults to Gtk.Window.get_default_icon_list()
logo-icon-name
str
r/w/en
A named icon to use as the logo for the about box.
program-name
str
r/w/en
The name of the program. If this is not set, it defaults to GLib.get_application_name()
translator-credits
str
r/w/en
Credits to the translators. This string should be marked as translatable
version
str
r/w/en
The version of the program
website
str
r/w/en
The URL for the link to the website of the program
website-label
str
r/w/en
The label for the link to the website of the program
wrap-license
bool
r/w/en
Whether to wrap the license text.
Signals
Name
Short Description
activate-link
The signal which gets emitted to activate a URI.
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/27# section 050
TITLE = "AboutDialog"
DESCRIPTION = """
The Gtk.AboutDialog offers a simple way to display information about a program like its
logo, name, copyright, website and license. It is also possible to give credits to the authors,
documenters, translators and artists who have worked on the program.
An about dialog is typically opened when the user selects the About option from the Help menu.
All parts of the dialog are optional.
"""import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
import webbrowser
classAboutDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="AboutDialog Demo")
self.set_border_width(10)
self.set_default_size(200, 100)
button = Gtk.Button("About")
button.set_size_request(80, 30)
button.connect("clicked", self.on_clicked)
fix = Gtk.Fixed()
fix.put(button, 20, 20)
self.add(fix)
self.show_all()
defon_clicked(self, widget):
about = Gtk.AboutDialog()
about.set_program_name("Battery")
about.set_version("0.1")
about.set_copyright("(c) Jan Bodnar")
about.set_comments("Battery is a simple tool for battery checking")
about.set_website("http://www.yuxiaosan.tk")
about.set_logo(Gtk.IconTheme.get_default().load_icon("battery", 48, 0))
about.add_credit_section("Credit", ["yuxiaosan", "Tom"])
about.connect("activate-link", self.click_link)
about.run()
about.destroy()
@staticmethoddefclick_link(widget, url):
webbrowser.open(url)
returnTruedefmain():
win = AboutDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/27# section 051
TITLE = "AppChooserDialog"
DESCRIPTION = """
Gtk.AppChooserDialog shows a Gtk.AppChooserWidget inside a Gtk.Dialog.
"""import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Gio
classAppChooserDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="AppChooserDialog Demo")
self.set_border_width(10)
self.set_default_size(200, 100)
button = Gtk.Button("Select APP")
button.connect("clicked", self.on_clicked)
fix = Gtk.Fixed()
fix.put(button, 50, 30)
self.add(fix)
self.show_all()
@staticmethoddefon_clicked(widget):
dialog = Gtk.AppChooserDialog(gfile=Gio.File.new_for_path("../../../Data/apple.png"))
response = dialog.run()
dialog.destroy()
defmain():
win = AppChooserDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Gtk.FileChooserDialog
继承关系
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/16# section 052
TITLE = "FileChooserDialog"
DESCRIPTION = """
Gtk.FileChooserDialog is a dialog box suitable for use with “File/Open” or “File/Save as” commands.
This widget works by putting a Gtk.FileChooserWidget inside a Gtk.Dialog
"""import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
classFileChooserWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="FileChooserDialog Example")
box = Gtk.Box(spacing=6)
self.add(box)
button1 = Gtk.Button("Choose File")
button1.connect("clicked", self.on_file_clicked)
box.add(button1)
button2 = Gtk.Button("Choose Folder")
button2.connect("clicked", self.on_folder_clicked)
box.add(button2)
defon_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))
self.add_filters(dialog)
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()
@staticmethoddefadd_filters(dialog):
filter_text = Gtk.FileFilter()
filter_text.set_name("Text files")
filter_text.add_mime_type("text/plain")
dialog.add_filter(filter_text)
filter_py = Gtk.FileFilter()
filter_py.set_name("Python files")
filter_py.add_mime_type("text/x-python")
dialog.add_filter(filter_py)
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dialog.add_filter(filter_any)
defon_folder_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a folder", self,
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
"Select", Gtk.ResponseType.OK))
dialog.set_default_size(800, 400)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Select clicked")
print("Folder selected: " + dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
defmain():
win = FileChooserWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Gtk.RecentChooserDialog
继承关系
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/27# section 053
TITLE = "RecentChooseDialog"
DESCRIPTION = """
Gtk.RecentChooserDialog is a dialog box suitable for displaying the recently used documents.
This widgets works by putting a Gtk.RecentChooserWidget inside a Gtk.Dialog.
It exposes the Gtk.RecentChooserIface interface, so you can use all the Gtk.RecentChooser
functions on the recent chooser dialog as well as those for Gtk.Dialog.
"""import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Pango
import os
import urllib.parse
classRecentChooserDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="RecentChooserDialog Demo")
self.set_border_width(10)
self.set_default_size(200, 100)
button = Gtk.Button("RecentChoose")
button.connect("clicked", self.on_clicked)
fix = Gtk.Fixed()
fix.put(button, 50, 30)
self.add(fix)
self.show_all()
defon_clicked(self, widget):
dialog = Gtk.RecentChooserDialog(title="Recent Documents", parent=self, manager=None,
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT))
response = dialog.run()
if response == Gtk.ResponseType.ACCEPT:
info = dialog.get_current_item()
uri = urllib.parse.urlsplit(info.get_uri(), "file").path
# utf-8
uri = urllib.parse.unquote(uri)
print(uri)
if os.path.exists(uri):
if os.path.isfile(uri):
print(open(uri, encoding="utf-8").readlines())
else:
print("it's a dir")
else:
print("file not exists")
dialog.destroy()
defmain():
win = RecentChooserDialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
自定义Dialog
例子
代码
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/16# section 054
TITLE = "CustomDialog"
DESCRIPTION = ""import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
classDialogExample(Gtk.Dialog):def__init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size(150, 100)
label = Gtk.Label("This is a dialog to display additional information")
box = self.get_content_area()
box.add(label)
box.add(Gtk.Button("button"))
box.add(Gtk.Entry())
self.show_all()
classDialogWindow(Gtk.Window):def__init__(self):
Gtk.Window.__init__(self, title="Dialog Example")
self.set_border_width(6)
button = Gtk.Button("Open dialog")
button.connect("clicked", self.on_button_clicked)
self.add(button)
defon_button_clicked(self, widget):
dialog = DialogExample(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("The OK button was clicked")
elif response == Gtk.ResponseType.CANCEL:
print("The Cancel button was clicked")
dialog.destroy()
defmain():
win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()