pythonwxpython菜鸟教程_wxpython新⼿向教程
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
可以利⽤ = 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 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’,
python菜鸟教程文档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()`

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论