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

这回该把定时提醒改成有界面的啦,顺带说下 我用了下程序,发现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 geometry 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()
		

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值