from Tkinter import *
Bu=Tk()
#回调函数
def PrintButton():
print '荷塘花!'
img=PhotoImage(file='D:/temp/1.gif')
Button(Bu,width=200,height=200,text='press',anchor='c',bg='blue',fg='red',padx=120,pady=120,borderwidth=10,relief='ridge',image=img,compound='bottom',command=PrintButton).pack()
Bu.mainloop()
anchor属性:
from Tkinter import *
root = Tk()
for a in ['n','s','e','w','ne','nw','se','sw']:
Button(root,
text = 'anchor',
anchor = a,
width = 30,
height = 4).pack()
#文本显示的位置。
Button(root,text = 'anchor',width = 30,height =4).pack()
Button(root,text = 'anchor',anchor = 'center',width = 30,height =4).pack()
Button(root,text = 'anchor',anchor = 'n',width = 30,height = 4).pack()
Button(root,text = 'anchor',anchor = 's',width = 30,height = 4).pack()
Button(root,text = 'anchor',anchor = 'e',width = 30,height = 4).pack()
Button(root,text = 'anchor',anchor = 'w',width = 30,height = 4).pack()
Button(root,text = 'anchor',anchor = 'ne',width = 30,height = 4).pack()
Button(root,text = 'anchor',anchor = 'nw',width = 30,height = 4).pack()
Button(root,text = 'anchor',anchor = 'se',width = 30,height = 4).pack()
Button(root,text = 'anchor',anchor = 'sw',width = 30,height = 4).pack()
root.mainloop()
利用按钮退出Label计时器:
def after(self, ms, func=None, *args):
"""Call function once after given time. MS specifies the time in milliseconds. FUNC gives the function which shall be called. Additional parameters are given as parameters to the function call. Return identifier to cancel scheduling with after_cancel."""
after(self, ms, func=None, *args) Tkinter的方法。标签实例
在给定时间后调用函数。MS以毫秒为单位指定时间。函数给出了响应调用的函数。额外的参数作为函数调用的参数。返回使用after_cancel取消调度的标识符。 if not func: # I'd rather use time.sleep(ms*0.001) self.tk.call('after', ms) else: def callit(): try: func(*args) finally: try: self.deletecommand(name) except TclError: pass callit.__name__ = func.__name__ name = self._register(callit) return self.tk.call('after', ms, name)
回调函数与函数:fun与fun()作为参数时表示的意义不同。
fun作为参数表示是函数
fun()作为参数时表示一个值
config(self, cnf=None, **kw) Tkinter方法。标签实例
配置小部件的资源。资源的值被指定为关键字。
就是使用config来重新给标签属性赋值
程序暂停的几种方法:
1、导入os模块
import os
os.system('pause)
2、导入subprocess模块
import subprocess
subprocess.call("pause",shell=True)
3、input();
这种方法不用包含模块,因此这也是最常用的一种暂停手段。
Python2中的raw_input()和input()语句在Python3中已经被合并到input()中。
程序退出方法:
1、导入os模块
import os
os._exit()
os._exit()会直接将python程序终止,之后的所有代码都不会继续执行。
2、导入sys模块
import sys
sys.exit()
sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获,那么python解释器将会退出。如果有捕获此异常的代码,那么这些代码还是会执行。
计时器示例:
from Tkinter import *
import subprocess
import os
import sys
counter = 0
def counter_label(label):
counter = 0
def count():
global counter
counter += 1
#配置属性
#区间大小
label.config(width=10, height=2)
#文本内容
label.config(text=str(counter/3600%24/10)+str(counter/3600%24%10)+':'+str(counter/60%60/10)+str(counter/60%60%10)+':'+str(counter%60/10)+str(counter%60%10))
#字体颜色
label.config(fg='red')
#label位置
label.config(anchor='c')
#after函数的第一个参数设置毫秒数后,调用count函数
label.after(1, count)
count()
def Pause():
# subprocess.call("pause",shell=True)
# os.system('pause')
# input()
sys.exit()
root = Tk()
root.title("计时器")
label = Label(root)
label.pack(side='left')
#查找方法或属性
# print help(label.config)
# print help(label.after)
counter_label(label)
button = Button(root, text='Stop', width=5, command=Pause,anchor='c').pack(side='right')#或command=root.destory小伙窗口
root.mainloop()
参考:http://blog.csdn.net/liuxu0703/article/details/60639166
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/110331.html