Python 中的 TK编程

时间:2023-03-08 17:24:10
Python 中的 TK编程

可爱的 Python:Python 中的 TK编程

http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-12/

python checkbox 用法详解

http://www.android100.org/html/201407/13/39698.html

Tkinter GUI编程——pack

http://blog.sina.com.cn/s/blog_4b5039210100epkl.html

环境安装:
sudo apt-get install python-t

python tkinter 窗口居中对齐:

     from Tkinter import *
def center_window(w=300, h=200):
# get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# calculate position x, y
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root = Tk()
center_window(500, 300)
root.mainloop() https://www.centos.bz/2013/01/python-tkinter-center-windows/
滚动条的使用:
 python入门
http://blog.chinaunix.net/uid-13231588-id-2904121.html http://tkinter.unpythonic.net/wiki/ScrolledFrame
A ScrolledFrame, with several bugs.

python使用Tk时固定窗口大小:

 from Tkinter import *
root=Tk()
root.minsize(50,50)
root.maxsize(150,150)
Label(root,text="I'm lly365").pack()
root.mainloop()

python使用Tkinter做Gui如何显示用forget隐藏的控件:

创建控件
bt=tkinter.Button(root,text='button')
bt.place(x=5,y=5)
bt.place_forget() #隐藏控件 再次bt.place() 还有传入x=5,y=5 不然不会显示 http://bbs.csdn.net/topics/320137780

 #!/usr/bin/env python
# coding: utf-8 import os
import sys
import string
import operator
import re
import threading
import csv import Tkinter # import the Tkinter module from time import sleep,ctime
from collections import defaultdict
from collections import Counter
from Tkinter import * from Tkinter import * COL_COUNT=8
GM_KEYS = set(
vars(Tkinter.Place).keys() +
vars(Tkinter.Pack).keys() +
vars(Tkinter.Grid).keys()
) class ScrolledFrame(object):
_managed = False
# XXX These could be options
x_incr = 5
y_incr = 5 def __init__(self, master=None, **kw):
self.width = kw.pop('width', 200)
self.height = kw.pop('height', 200) self._canvas = Tkinter.Canvas(master, **kw)
self.master = self._canvas.master
self._hsb = Tkinter.Scrollbar(orient='horizontal',
command=self._canvas.xview)
self._vsb = Tkinter.Scrollbar(orient='vertical',
command=self._canvas.yview)
self._canvas.configure(
xscrollcommand=self._hsb.set,
yscrollcommand=self._vsb.set) self._placeholder = Tkinter.Frame(self._canvas)
self._canvas.create_window(0, 0, anchor='nw', window=self._placeholder) self._placeholder.bind('<Map>', self._prepare_scroll)
for widget in (self._placeholder, self._canvas):
widget.bind('<Button-4>', self.scroll_up)
widget.bind('<Button-5>', self.scroll_down) def __getattr__(self, attr):
if attr in GM_KEYS:
if not self._managed:
# Position the scrollbars now.
self._managed = True
if attr == 'pack':
self._hsb.pack(side='bottom', fill='x')
self._vsb.pack(side='right', fill='y')
elif attr == 'grid':
self._hsb.grid(row=1, column=0, sticky='ew')
self._vsb.grid(row=0, column=1, sticky='ns')
return getattr(self._canvas, attr) else:
return getattr(self._placeholder, attr)
def yscroll(self, *args):
self._canvas.yview_scroll(*args)
def scroll_up(self, event=None):
self.yscroll(-self.y_incr, 'units')
def scroll_down(self, event=None):
self.yscroll(self.y_incr, 'units')
def see(self, event):
widget = event.widget
w_height = widget.winfo_reqheight()
c_height = self._canvas.winfo_height()
y_pos = widget.winfo_rooty() if (y_pos - w_height) < 0:
# Widget focused is above the current view
while (y_pos - w_height) < self.y_incr:
self.scroll_up()
self._canvas.update_idletasks()
y_pos = widget.winfo_rooty()
elif (y_pos - w_height) > c_height:
# Widget focused is below the current view
while (y_pos - w_height - self.y_incr) > c_height:
self.scroll_down()
self._canvas.update_idletasks()
y_pos = widget.winfo_rooty() def _prepare_scroll(self, event):
frame = self._placeholder
frame.unbind('<Map>') if not frame.children:
# Nothing to scroll.
return
for child in frame.children.itervalues():
child.bind('<FocusIn>', self.see) width, height = frame.winfo_reqwidth(), frame.winfo_reqheight()
self._canvas.configure(scrollregion=(0, 0, width, height),
yscrollincrement=self.y_incr, xscrollincrement=self.x_incr)
self._canvas.configure(width=self.width, height=self.height) def test_04():
root = Tkinter.Tk() center_window(root,850,580)
root.minsize(850,580)
root.maxsize(850,580)
sf = ScrolledFrame()
sf.grid(row=0, column=0, sticky='nsew')
sf.master.grid_columnconfigure(0, weight=1)
sf.master.grid_rowconfigure(0, weight=1) for _ in range(10):
lbl = Tkinter.Label(sf, text="Hi")
lbl.pack()
btn = Tkinter.Button(sf, text="Buh")
btn.pack()
entry = Tkinter.Entry(sf)
entry.pack() root.mainloop() def test_05():
root = Tkinter.Tk() # create a root window
root.title('demux and protocol of the ffmpeg config')
states = []
#root['width']=1000
#root['height']=500
center_window(root,850,580)
root.minsize(850,580)
root.maxsize(850,580)
sf = ScrolledFrame()
sf.grid(row=0, column=0, sticky='nsew')
sf.master.grid_columnconfigure(0, weight=1)
sf.master.grid_rowconfigure(0, weight=1)
Label(sf,text = 'please select demux type:').place(x=5,y=10)
#frame = Frame(root, width=500, height=400, bd=1)
#frame.pack()
#iframe1 = Frame(frame, bd=2, relief=SUNKEN)
chk_demux_list=[]
for i in range(10):
var = IntVar()
chk = Checkbutton(sf, text=str(i), variable=var)
if i+1<=COL_COUNT:
chk.place(x=i*100+30,y=10+30*(i/COL_COUNT+1))
else:
chk.place(x=(i%COL_COUNT)*100+30,y=10+30*(i/COL_COUNT+1))
#chk.pack(side=LEFT)
states.append(var)
chk_demux_list.append(chk)
#Label(root,text = 'please select protocol type:').place(x=5,y=30+chk_demux_list[len(chk_demux_list)-1].winfo_y())
Label(sf,text = 'please select protocol type:').place(x=5,y=250)
root.mainloop() # create an event loop
#print states
#for var in states:
# print var.get()
#print map((lambda var: var.get()), states)
def center_window(root,w=300, h=200):
# get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# calculate position x, y
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
#80x80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
def test_02():
root = Tkinter.Tk() # create a root window
root.title('demux and protocol of the ffmpeg config')
states = []
#root['width']=1000
#root['height']=500
Label(root,text = 'please select demux type:').place(x=5,y=10)
center_window(root,850,580)
root.minsize(850,580)
root.maxsize(850,580)
#frame = Frame(root, width=500, height=400, bd=1)
#frame.pack()
#iframe1 = Frame(frame, bd=2, relief=SUNKEN)
chk_demux_list=[]
for i in range(100):
var = IntVar()
chk = Checkbutton(root, text=str(i), variable=var)
if i+1<=COL_COUNT:
chk.place(x=i*100+30,y=10+30*(i/COL_COUNT+1))
else:
chk.place(x=(i%COL_COUNT)*100+30,y=10+30*(i/COL_COUNT+1))
#chk.pack(side=LEFT)
states.append(var)
chk_demux_list.append(chk)
#Label(root,text = 'please select protocol type:').place(x=5,y=30+chk_demux_list[len(chk_demux_list)-1].winfo_y())
Label(root,text = 'please select protocol type:').place(x=5,y=250)
root.mainloop() # create an event loop
#print states
#for var in states:
# print var.get()
#print map((lambda var: var.get()), states) def test_03():
root = Tk()
root.title("Note Taker")
def Button1():
listbox.insert(END, "button1 pressed")
def Button2():
listbox.insert(END, "button2 pressed")
def Button3():
text_contents = text.get()
listbox.insert(END, text_contents)
text.delete(0,END)
button1 = Button(root, text="button1", command = Button1)
button2 = Button(root, text="button2", command = Button2)
button3 = Button(root, text="button3", command = Button3)
text = Entry(root)
scrollbar = Scrollbar(root, orient=VERTICAL)
listbox = Listbox(root, yscrollcommand=scrollbar.set)
scrollbar.configure(command=listbox.yview)
text.pack()
button1.pack()
button2.pack()
button3.pack()
listbox.pack()
scrollbar.pack()
root.mainloop() def test_01():
#content ==> ###pos=350143600,pts=2676718###
#filename="F:\\yingc\\work\\goxceed-dvbs-hd\\6605\\solution\\aa"
filename="./aa"
pos=-1
dts=-1
poslist=[]
dtslist=[] str1="###pos="
str2=",pts=" f = open(filename)
for line in f:
aa=line[0:len(str1)]
if aa == str1:
pos=line[len(str1):line.index(str2)]
dts=line[line.index(str2)+len(str2):len(line)-3-1]
poslist.append(pos)
dtslist.append(dts)
f.close() #s=[11,22,11,44,22,33]
d = defaultdict(list)
for k,va in [(v,i) for i,v in enumerate(poslist)]:
d[k].append(va)
#print d.items()
count=0
for value in d.items():
if len(value[1])>1:
print value
count=count+1
print "poslen:"+str(len(poslist))+",dtslen"+str(len(dtslist))
print str(len(d))+","+str(count) #d = defaultdict(list)
#for k,va in [(v,i) for i,v in enumerate(dtslist)]:
# d[k].append(va)
##print d.items()
#for value in d.items():
# if len(value[1])>4:
# print value ##print Counter([11,22,11,44,22,33]) if __name__ == "__main__":
test_02()
print "finish"
Python:tkinter-Parent获取弹出窗口的返回值
http://blog.sina.com.cn/s/blog_ac9fdc0b0101naol.html
Tkinter里没有messagebox,
在Python2.5里,要从 tkMessageBox导入
from tkMessageBox import * 然后直接用showinfo()来实现输出 showinfo(message=“hello”) 这个时候不需要使用任何窗口路径来调用它,直接调用它就可以了。

aa