Python 面向对象程序设计——GUI学习

常见GUI库:Tkinter(高效)、PyQt(强大)、wxPython

这里选择wxPython

相关文档https://www.wxpython.org/pages/overview/#hello-world

pip install -i https://pypi.doubanio.com/simple/ wxPython

基本结构:

# First things, first. Import the wxPython package.
import wx

# Next, create an application object.
app = wx.App()

# Then a frame.
frm = wx.Frame(None, title="Hello World")

# Show it.
frm.Show()

# Start the event loop.
app.MainLoop()
import wx
class APP(wx.App):
    def Oninit(self):  #创建应用程序初始化方法
        frame=wx.Frame(None,title='hollo world')#创建窗口,定义标题栏.
        frame.Show(True)# 显示循环
        return True
    
if __name__ == '__main__':
    app=APP()  #创建应用对象.应用程序对象也可以是子类的一个实例
    app.MainLoop()

设置窗体,显示文本

import wx
class Frame1(wx.Frame):
    def __init__(self,superior): 
        wx.Frame.__init__(self,parent=superior,title='Example',pos=(100,200),size=(350,200))
        panel=wx.Panel(self)
        text=wx.TextCtrl(panel,value='hello world!',size=(350,200))
    
if __name__ == '__main__':
    app=wx.App()
    frame=Frame1(None)
    frame.Show(True)
    app.MainLoop()

定义鼠标左键抬起事件绑在panel的OnClink()

import wx
class Frame1(wx.Frame):
    #定义鼠标左键抬起事件绑在panel的OnClink()
    def __init__(self,superior): 
        wx.Frame.__init__(self,parent=superior,title='Example',pos=(100,200),size=(350,200))
        self.panel=wx.Panel(self)
        self.panel.Bind(wx.EVT_LEFT_UP,self.OnClink)
        #text=wx.TextCtrl(panel,value='hello world!',size=(350,200))
    def OnClink(self,event):
        posm=event.GetPosition()
        wx.StaticText(parent=self.panel,label='hello world!',pos=(posm.x,posm.y))
    
if __name__ == '__main__':
    app=wx.App()
    frame=Frame1(None)
    frame.Show(True)
    app.MainLoop()

发布了50 篇原创文章 · 获赞 14 · 访问量 7959

猜你喜欢

转载自blog.csdn.net/yeyuanxiaoxin/article/details/104523540
今日推荐