glade画的gtk界面python里能直接调用

linux下假如没有安装相应库就自己命令装一下

sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-3.0

来一段代码验证下

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

window = Gtk.Window(title="Test Window")
window.show()
window.connect("destroy", Gtk.main_quit)
Gtk.main()

能弹出窗口说明环境没问题,再来一段代码

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class SimpleGUI:
    def __init__(self):
        # 创建一个窗口
        self.window = Gtk.Window(title="python GTK+ Example")
        self.window.set_default_size(600,300)
        self.window.connect("delete_event", Gtk.main_quit)

        # 创建一个标签
        self.label = Gtk.Label(label="Welcome to PyGtk!")

        # 创建一个按钮
        self.button = Gtk.Button(label="Click Me!")
        self.button.connect("clicked", self.on_button_clicked)
        # 创建退出按钮
        self.btnExit = Gtk.Button(label="退出")
        self.btnExit.connect("clicked", self.on_btnExit_clicked)
        # 创建一个垂直布局
        self.vbox = Gtk.VBox()
        self.vbox.pack_start(self.label, True, True, 0)
        self.vbox.pack_start(self.button, True, True, 0)
        self.vbox.pack_start(self.btnExit, True, True, 0)
        # 设置窗口的内容
        self.window.add(self.vbox)
        self.window.show_all()

    def on_button_clicked(self, widget):
        self.label.set_text("Button Clicked!")
    def on_btnExit_clicked(self, widget):
        self.window.close()    

if __name__ == "__main__":
    gui = SimpleGUI()
    Gtk.main()

说明python和gtk配合很好,下面就用glade画一个界面 test.glade




  
  
    False
    center-always
    True
    测试界面
    
    
      
    
    
      
        True
        False
        
          
            退出
            100
            40
            True
            True
            True
            
          
          
            425
            350
          
        
        
          
            100
            80
            True
            True
            
          
          
            200
            200
          
        
        
          
            100
            80
            True
            False
            label
          
          
            200
            100
          
        
        
          
            测试
            100
            40
            True
            True
            True
            
          
          
            150
            350
          
        
      
    
  

然后到python里调用 pygtkmain.py

#!/usr/bin/python3
#-*- coding:utf-8 -*-
# support by [email protected] 2023 

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class pygtkmain():
    """This is the pygtkmain application"""
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("test.glade")
        self.window = self.builder.get_object("form1")
        self.window.set_default_size(600,480)
        self.window.set_position(1)
        self.window.show()
        self.window.connect("destroy", Gtk.main_quit)
        self.text1 = self.builder.get_object("text1")
        self.label1 = self.builder.get_object("label1")
        self.btnTest = self.builder.get_object("btnTest")
        self.btnTest.connect("clicked", self.ontest)
        self.btnExit = self.builder.get_object("btnExit")
        self.btnExit.connect("clicked", self.onexit)
    def ontest(self, event=None):
        self.label1.set_text(self.text1.get_text())
    def onexit(self, event=None):
        self.window.close()

if __name__ == "__main__":
    winf = pygtkmain()
    Gtk.main()

运行顺利,以后python整带界面的就可以照这个思路整了。

你可能感兴趣的:(python,linux,python,gtk,glade)