wxpython教程
- 手写wxpython会有各种问题,可以去下载wxformbuilder布局管理器
然后去百度一下这个布局管理器的教程,这里太多了不多赘述。 - 两种创建窗口方式:class MyFrame(wx.Frame)和class MyPanel(wx.Panel)来继承wx.Frame和wx.Panel,但是用wxformbuilder来创建布局和窗口会自动帮忙写好。
继承wx.Frame的结尾是app=wx.App() frm=MyFrame() frm.Show() app.MainLoop()
Show中的参数为True/False,False则不可见窗口
继承wx.Panel的结尾是app = wx.App() frame = wx.Frame(None) panel = MyPanel(frame) frame.Show() app.MainLoop()
MainLoop为每个继承类都要填写,否则无法运行
基本的控件TextCtrl,Button,StaticText
TextCtrl
基本的属性Clear()(清空TextCtrl中的内容),AppendText()(在末尾添加文本)
排列布局在wxformbuilder中都有举几个列子(wx.TE_MULTILNE为可以进行多行输入,wx.TE_LEFT为左对齐,可以用 | 来进行多种styel组合的方式)
Button
wx.Button来创建按钮
按钮调用函数
可以这么写self.Button.Bind(self,self.func)但是在wxformbuilder进行设置事件,它会写好的
StaticText
可以利用self.tc = wx.StaticText(self, 0,’ str ')
第三个参数为标签内容
以上都是在 class MyPanel(wx.Panel):的理论上进行的,控件最好都是在Panel上进行控制和编写!!
panel的创建方式wx.Panel.init(self, parent)
frame的创建方式 wx.Frame.init(self,None,title=“str”)
以上都可以用wxformbuilder来完成!
下面插入我的一段作业代码``
#-- coding: utf-8 --
import wx
import wx.xrc
import time
import random
import threading
‘’‘利用threading解决while死循环会卡死程序的问题,’’’
###########################################################################
Class MyPanel1
###########################################################################
dict = {1: ‘name’,
2: ‘name’,
3: ‘name’,
4: ‘name’,
5: 'name‘,
6: ‘name’,
7: ‘name’,
8: ‘name’,
9: ‘name’,
10: ‘name’,
11: 'name’,
12: ‘name’,
13: ‘name’,
14: ‘name’,
15: ‘name’,
16: ‘name’,
17: ‘name’,
18: ‘name’,
19: ‘name’,
20: ‘name’,
21: ‘name’,
22: ‘name’,
23: ‘name’,
24: ‘name’,
25: ‘name’,
26: ‘name’,
27: ‘name’,
28: ‘name’}
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(500, 300),
style=wx.TAB_TRAVERSAL)
self.Sure = False
bSizer1 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText1.Wrap(-1)
bSizer1.Add(self.m_staticText1, 1, wx.ALL | wx.EXPAND, 5)
gSizer1 = wx.GridSizer(0, 2, 0, 0)
self.StartButton = wx.Button(self, wx.ID_ANY, u"Start", wx.DefaultPosition, wx.DefaultSize, 0)
gSizer1.Add(self.StartButton, 0, wx.ALL, 5)
self.StopButton = wx.Button(self, wx.ID_ANY, u"Stop", wx.DefaultPosition, wx.DefaultSize, 0)
gSizer1.Add(self.StopButton, 0, wx.ALL | wx.ALIGN_RIGHT, 5)
bSizer1.Add(gSizer1, 1, wx.EXPAND, 5)
self.SetSizer(bSizer1)
self.Layout()
# Connect Events
self.StartButton.Bind(wx.EVT_BUTTON, self.StartButtonOnButtonClick)
self.StopButton.Bind(wx.EVT_BUTTON, self.StopButtonOnButtonClick)
def __del__(self):
pass
# Virtual event handlers, overide them in your derived class
def StartButtonOnButtonClick(self, event):
self.Sure = True
def StopButtonOnButtonClick(self, event):
self.Sure = False
def Round(self):
self.m_staticText1.SetLabel('现在开始抽取')
while True:
if self.Sure == True:
self.m_staticText1.SetLabel('{0}'.format(dict[random.randint(1,28)]))
time.sleep(0.1)
app = wx.App()
frame = wx.Frame(None)
panel = MyPanel(frame)
frame.Show()
task=threading.Thread(target=panel.Round)
task.start()
app.MainLoop()`
来源:oschina
链接:https://my.oschina.net/u/4377926/blog/4683950