您的位置:首页 > 编程语言 > Python开发

python实战教程1 定时提醒程序的制作(2)

2013-10-04 17:26 691 查看
这回该把定时提醒改成有界面的啦,顺带说下 我用了下程序,发现beep一下不够使,那么多print几下就好了

改成有界面的选择:

1 qt 这个之前用过很好用,起源好像是诺基亚的一个软件部门

2 windows api

3 自带的tk (graphical user interfaces wth tk)

 

机子已经被我配的乱七八糟了 不想多装东西 这次选用tk api牵扯一堆宏定义 偶尔用下还可以 整体用感觉不爽

先看document里面的tk一些示例,用idle去尝试

刚吃完饭,随便开个电影一边看一边尝试。。。

class Application(tk.Frame):
def __init__(self,master=None):
tk.Frame.__init__(self,master)
self.pack()
self.createWidgets()

def createWidgets(self):
self.hi_there=tk.Button(self)
self.hi_there["text"]="Hello World\n(click me)"
self.hi_there["command"]=self.say_hi
self.hi_there.pack(side="bottom")
self.QUIT=tk.Button(self,text="QUIT",fg="red",command=root.destroy)
self.QUIT.pack(side="bottom")
def say_hi(self):
print("hi there,everyone!")

root=tk.Tk()
app=Application(master=root)
app.mainloop()


分析示例程序 init应该为构造函数,pack不懂先跳过去,creatWidgets就是制造界面的控件

看createWidgets内部,先是构造了一个hi_there对象 button 上面的text command对应触发的事件

QUIT对象展示了另一种构造方法。

总结下我们现在不懂的地方可能就一个pack,先把这个搞定然后联想我们之前的程序。

查看文档发现:

To give a widget to the
packer (geometry manager), you call
pack with optional arguments. In Tkinter, the
Pack class holds all this functionality, and the various forms of the
pack command are implemented as methods. All widgets in
tkinter are subclassed from thePacker, and so inherit all thepacking
methods. See thetkinter.tix module documentation for additional
information on the Form g
4000
eometry manager

pack为构造控件地理的函数。我们可以先跳过。

我们现在要做的事情是构造出来界面和我们的timerNote相关联。

我们先试着做一个start和stop控件,然后去寻找一个可以输入数字或者字符串等的控件。

class Application(tk.Frame):
def __init__(self,master=None):
tk.Frame.__init__(self,master)
self.pack()
self.createWidgets()

def createWidgets(self):
self.start=tk.Button(self)
self.start["text"]="start"
self.start["command"]=self.start
self.start.pack(side="top")

self.end=tk.Button(self,text="end",fg="red",command=self.end)
self.end.pack(side="bottom")
def start(self):
print("start button clicked!")
def end(self):
print("end button clicked!")

root=tk.Tk()
app=Application(master=root)
app.mainloop()

可以触发两个按钮的程序,在此发现点击start不起作用,查询。。。

csdn论坛询问,原来是粗心 变量名和函数名一样了

end起作用是因为在end变量初始化在end函数之后

更正后的代码:

 

class Application(tk.Frame):
def __init__(self,master=None):
tk.Frame.__init__(self,master)
self.pack()
self.createWidgets()

def createWidgets(self):

self.end=tk.Button(self,text="end",fg="red",command=self.cbend)
self.end.pack(side="bottom")

self.start=tk.Button(self)
self.start["text"]="start"
self.start["command"]=self.cbstart
self.start.pack(side="top")

def cbstart(self):
print("start button clicked!")
def cbend(self):
print("end button clicked!")

root=tk.Tk()
app=Application(master=root)
app.mainloop()

现在两个按键都可以使用了

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: