python gtk_python-gtk学习笔记

傅兴平
2023-12-01

本篇博客不定期更新,最后更新时间:2021年1月21号

1. 创建一个简单的Window

import gi

gi.require_version("Gtk","3.0")

from gi.repository import Gtk

class MyWindow(Gtk.Window):

def __init__(self):

super(MyWindow,self).__init__(title="mywindow")

win = MyWindow()

win.connect("destroy",Gtk.main_quit)

win.show_all()

Gtk.main()

上面的代码只是创建了一个空白的窗口,什么都没有。

窗口出现时,大小已经确认,现在调整窗口的大小。

代码如下:

import gi

gi.require_version("Gtk","3.0")

from gi.repository import Gtk

class MyWindow(Gtk.Window):

def __init__(self):

super(MyWindow,self).__init__(title="mywindow")

self.set_default_size(800,800)

win = MyWindow()

win.connect("destroy",Gtk.main_quit)

win.show_all()

Gtk.main()

2. 为Window添加一个Box

下面代码为window添加一个box,box里面放置一个button,button的标签是“点击”

import gi

gi.require_version("Gtk","3.0")

from gi.repository import Gtk

class MyWindow(Gtk.Window):

def __init__(self):

super(MyWindow,self).__init__(title="mywindow")

self.set_default_size(800,800)

#创建一个box,并添加到window

self.box = Gtk.Box(spacing=8)

self.add(self.box)

#创建一个button,并置于box中

self.button = Gtk.Button(label="点击")

self.box.pack_start(self.button,True,True,0)

win = MyWindow()

win.connect("destroy",Gtk.main_quit)

win.show_all()

Gtk.main()

上面的代码中,为这个window创建了一个box,并在box里面放置了一个按钮,按钮的标签是“点击”

但是用鼠标点击”点击“按钮时,没有什么变化。

现在给”点击”按钮加一个事件,即点击了“点击”之后,会发生什么。

代码如下:

import gi

gi.require_version("Gtk","3.0")

from gi.repository import Gtk

class MyWindow(Gtk.Window):

def __init__(self):

super(MyWindow,self).__init__(title="mywindow")

self.set_default_size(800,800)

#创建一个box,并添加到window

self.box = Gtk.Box(spacing=8)

self.add(self.box)

#创建一个button,并置于box中

self.button = Gtk.Button(label="点击")

self.box.pack_start(self.button,True,True,0)

#这里配置点击事件

self.button.connect("clicked",self.on_button)

#这里定义点击后的动作

def on_button(self,widget):

print("点击我了")

win = MyWindow()

win.connect("destroy",Gtk.main_quit)

win.show_all()

Gtk.main()

3. 为Window添加一个Grid

import gi

gi.require_version("Gtk","3.0")

from gi.repository import Gtk

class MyWindow(Gtk.Window):

def __init__(self):

super(MyWindow,self).__init__(title="mywindow")

self.set_default_size(800,800)

#创建一个grid,并添加到window

self.grid = Gtk.Grid()

self.add(self.grid)

#创建4个button,并置于grid中

self.button_01 = Gtk.Button(label="按钮1")

self.button_02 = Gtk.Button(label="按钮2")

self.button_03 = Gtk.Button(label="按钮3")

self.button_04 = Gtk.Button(label="按钮4")

self.grid.attach(self.button_01,0,0,1,1)

self.grid.attach(self.button_02,0,1,1,1)

self.grid.attach_next_to(self.button_03,self.button_02,Gtk.PositionType.RIGHT,1,1)

self.grid.attach_next_to(self.button_04,self.button_02,Gtk.PositionType.BOTTOM,2,2)

win = MyWindow()

win.connect("destroy",Gtk.main_quit)

win.show_all()

Gtk.main()

4. 为window添加ListBox

import gi

gi.require_version("Gtk","3.0")

from gi.repository import Gtk

class MyListBoxRow(Gtk.Window):

def __init__(self):

super(MyListBoxRow,self).__init__(title="my_list_box_row")

self.set_default_size(800,800)

#先创建一个box,用于下面放置listbox,指定其位置

self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,spacing=6)

self.add(self.box)

#创建一个ListBox

self.listbox = Gtk.ListBox()

self.listbox.set_selection_mode(Gtk.SelectionMode.NONE)

self.box.pack_start(self.listbox,True,True,0) #通过box放置listbox

#创建一个ListBoxRow,并至于ListBox中

first_list_box = Gtk.ListBoxRow()

#创建一个box,用于在ListBoxRow中放置其他对象

hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=50)

#把box添加到ListBoxRow中

first_list_box.add(hbox)

label = Gtk.Label(label="开机启动",xalign=0)

switch = Gtk.Switch()

switch.connect("notify::active",self.on_switch_activated)#绑定事件

hbox.pack_start(label,True,True,0)

hbox.pack_start(switch,False,True,0)

self.listbox.add(first_list_box)

#创建第二个ListBoxRow,并置于ListBox中

second_list_box = Gtk.ListBoxRow()

#创建box,用于在ListBoxRow中放置其他对象

hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=50)

second_list_box.add(hbox)

label = Gtk.Label(label="是否启用同步",xalign=0)

check = Gtk.CheckButton()

check.connect("toggled",self.on_check_button)

hbox.pack_start(label,True,True,0)

hbox.pack_start(check,False,True,0)

self.listbox.add(second_list_box)

#创建第三个ListBoxRow,并至于ListBox中

third_list_box = Gtk.ListBoxRow()

#在ListBoxRow中添加Box,用于方式其他对象

hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=50)

third_list_box.add(hbox)

label = Gtk.Label(label="选择语言",xalign=0)

combo = Gtk.ComboBoxText()

combo.insert(0,"0","中文")

combo.insert(1,"1","English")

hbox.pack_start(label,True,True,0)

hbox.pack_start(combo,False,True,0)

self.listbox.add(third_list_box)

def on_switch_activated(self,switch,gparam):

if switch.get_active():

state = "on"

else:

state = "off"

print("switch was turned",state)

def on_check_button(self,check_button):

if check_button.get_active():

state = "check on"

else:

state = "check off"

print("check button turned",state)

win = MyListBoxRow()

win.connect("destroy",Gtk.main_quit)

win.show_all()

Gtk.main()

标签Label

标签是窗口内容里不可修改的对象。

#创建一个Label对象

import sys

import gi

gi.require_version("Gtk","3.0")

from gi.repository import Gtk

class MyWindow(Gtk.Window):

def __init__(self):

super(MyWindow,self).__init__(title=sys.argv[0])

self.set_default_size(300,300)

self.label = Gtk.Label()

self.label.set_text("liwl")

self.add(self.label)

win = MyWindow()

win.connect("destroy",Gtk.main_quit)

win.show_all()

Gtk.main()

Label对象方法

Gtk.Label.set_text()

Gtk.Label.set_markup()

Gtk.Label.set_selectable()

Gtk.Label.set_justify()

Gtk.Label.set_line_wrap()

Gtk.Label.new_with_mnemonic()

Gtk.Label.set_text_with_mnemonic()

 类似资料: