import Gtk 使用gtk 3模組
import gtk 使用gtk 2模組
Anjuta Ide建立Project,先用Glade建立gtk ui(滑鼠拖拖拉拉就建好了,前面文章有寫怎樣使用glade),src目錄下ui檔就是XML(如圖)
在src目錄下py檔,就是Python,程式,當初使用glade建3個button,我把信號處理,都設成SHOW(self, widget, *event) method,如何取得,當按下按鈕,SHOW(self, widget, *event) method如何得知,是那個按鈕被按下,self.window.get_focus(),意指本身,指向window,在指向激活對象,按鈕1被按下觸動SHOW method,self.window.get_focus()當然就是按鈕1,按鈕2被按下觸動SHOW method,self.window.get_focus()當然就是按鈕2,按鈕3被按下觸動SHOW method,self.window.get_focus()當然就是按鈕3,至於我為何知道用get_focus(),是看pygtk使用手冊,google找一下就有了,網路上也有很多Python教學
def SHOW(self, widget, *event):
#self指向button,widget指信號對象此為label1
#self.label.set_text(self.button2.get_label())
#get_focus可取得激活對象
bt_active_label=self.window.get_focus().get_label()
#widget是指信號處理對象,用GALADE創建UI時,是為label1
widget.set_text(bt_active_label)
#print(user_data)
#除錯技巧,印出
print(str(self.window.get_focus().get_label()))
#self.button1.set_label("JJJJJJ")
#重設label位置
#self.fixed.put(self.label, 80, 40)
整個程式碼(除了取得元件,和SHOW method是在加上,其餘都是IDE自動產生)
#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
#
# main.py
# Copyright (C) 2015 yplin
#
# pygtk-foobar_u1410 is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pygtk-foobar_u1410 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see
from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys
#Comment the first line and uncomment the second before installing
#or making the tarball (alternatively, use project variables)
UI_FILE = "src/pygtk_foobar_u1410.ui"
#UI_FILE = "/usr/local/share/pygtk_foobar_u1410/ui/pygtk_foobar_u1410.ui"
class GUI:
def __init__(self):
#新建了一個xml-gtk
self.builder = Gtk.Builder()
#把UI_FILE加到gtk,glade產生的xml
self.builder.add_from_file(UI_FILE)
#建立xml信號連結
self.builder.connect_signals(self)
#取得xml UI上所有元件
self.label=self.builder.get_object('label1')
self.button2=self.builder.get_object('button2')
self.button1=self.builder.get_object('button1')
self.button3=self.builder.get_object('button3')
self.window=self.builder.get_object('window')
self.fixed=self.builder.get_object('fixed1')
def on_window_destroy(self, widget, *event):
Gtk.main_quit()
def SHOW(self, widget, *event):
#self指向本身,widget指信號對象此為label1
#self.label.set_text(self.button2.get_label())
#get_focus可取得激活對象
bt_active_label=self.window.get_focus().get_label()
#widget是指信號處理對象,用GALADE創建UI時,是為label1
widget.set_text(bt_active_label)
#print(user_data)
#除錯技巧,印出
print(str(self.window.get_focus().get_label()))
#self.button1.set_label("JJJJJJ")
#重設label位置
#self.fixed.put(self.label, 80, 40)
def main():
app = GUI()
app.window.show()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
執行就像下面圖
沒有留言:
張貼留言