为即将到来的TJLUG的第三次线下活动写的一个抽奖小程序,随机生成1,200之间的号码,点击开始按钮开始滚动数字,停止按钮开始停止数字,并选择当前数字为中将号码
python源代码:
#-*- coding: utf-8 -*-
import sys
import os
import random
import time
import threading
try:
import pygtk
pygtk.require('2.0')
except:
pass
try:
import gtk
except:
print "GTK Not avaliable!"
sys.exit(1)
class update(threading.Thread):
def __init__(self):
"""初始化线程"""
threading.Thread.__init__(self,name="update")
self.label=None
self.num=0
self.over=False
def setNum(self,widget,data):
"""设置label的text值"""
widget.set_text(str(data))
return True
def kill(self):
"""设置标志位,来杀死线程"""
self.over=True
def run(self):
while not self.over :
self.num=random.randint(1,200)
self.setNum(self.label,self.num)
time.sleep(0.1000)
class lucky():
"""抽奖的一个小程序"""
def on_window_destroy(self,widget,data=None):
gtk.main_quit()
def __init__(self):
"""读取glade文件,并自动链接信号"""
#从xml文件中读取数据,并链接必要的信号
self.builder=gtk.Builder()
self.file=sys.path[0]+"/lucky.glade"
self.builder.add_from_file(self.file)
self.builder.connect_signals(self)
for widget in self.builder.get_objects():
if issubclass(type(widget),gtk.Buildable):
name=gtk.Buildable.get_name(widget)
setattr(self,name,widget)
#显示所有窗体
self.window.set_size_request(800,500)
self.window.show()
def on_startbutton_clicked(self,widget,data=None):
"""开始抽奖"""
self.u=update()
self.u.label=self.luckylabel
self.u.setDaemon(True)
self.u.start()
def on_stopbutton_clicked(self,widget,data=None):
"""停止,显示当前号码"""
self.u.kill()
#主循环
def main(self):
gtk.main()
if __name__=="__main__":
gtk.gdk.threads_init()
lc=lucky()
lc.main()
Glade源文件
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window">
<child>
<object class="GtkTable" id="table">
<property name="visible">True</property>
<property name="n_rows">3</property>
<property name="n_columns">3</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkButton" id="startbutton">
<property name="label" translatable="yes">start</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_startbutton_clicked"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
<property name="x_options">GTK_EXPAND</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<object class="GtkButton" id="stopbutton">
<property name="label" translatable="yes">stop</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_stopbutton_clicked"/>
</object>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
<property name="x_options"></property>
<property name="y_options"></property>
</packing>
</child>
<child>
<object class="GtkLabel" id="luckylabel">
<property name="visible">True</property>
<property name="label" translatable="yes">100</property>
<property name="justify">center</property>
<property name="ellipsize">middle</property>
<attributes>
<attribute name="weight" value="medium"/>
<attribute name="size" value="200000"/>
<attribute name="foreground" value="#e3970be40be4"/>
</attributes>
</object>
<packing>
<property name="right_attach">3</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
<object class="GtkTextBuffer" id="textbuffer1"/>
</interface>
发表在《
发表在《
发表在《
不错。有没胆写一个 百家乐 或者 德州扑克 模拟程序贴上来,造福一下广大赌民?
[回复]
freetstar
回复:
六月 19th, 2011 at 10:53 上午
哈哈,话说昨天晚上睡觉前蛋疼的想到来着
[回复]
南京这边直接用的是expr $RANDOM % 150
[回复]
freetstar
回复:
五月 18th, 2011 at 4:18 下午
原来就是Jasey_wang同学的blog里说的就是你
[回复]
Caspar
回复:
五月 19th, 2011 at 3:51 下午
@freetstar, 正好靠得近,就跑去帮忙了~
天津站的release party应该开始了吧,顺祝活动成功啊~~~
[回复]
freetstar
回复:
五月 19th, 2011 at 6:07 下午
@Caspar, 已经完了,随后我上图和总结
[回复]
最开始直接用代码画GUI,好长好长啊
,还是glade好用
[回复]
freetstar
回复:
五月 17th, 2011 at 11:54 上午
恩,还是Glade吧,多好
[回复]
话说 你这个代码高亮插件是啥? 感觉很不错./
[回复]
freetstar
回复:
五月 16th, 2011 at 1:00 下午
@OSMSG, 哥,你好像第2遍问了,highlighterenvolde
[回复]
glade不习惯,我还处在凭空想着,然后用C直接写GTK+
稍复杂点的就不行了
[回复]
freetstar
回复:
五月 11th, 2011 at 8:39 下午
@婉秋, 也可以用c直接来搞glade的,画UI还是用glade吧,多省事,还不用在代码里写UI..
[回复]
GUI程序?好长好长……
[回复]
freetstar
回复:
五月 11th, 2011 at 7:49 下午
@Tao Zhu, 恩阿,么有截图,其实很短….
[回复]