python tkinker 라 이브 러 리 모듈 상세 설명
15012 단어 Python
Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/untitled/python/web/tkinter.py", line 1, in import tkinter as tk File "C:\Users\Administrator\PycharmProjects\untitled\python\web\tkinter.py", line 2, in root=tk.Tk() AttributeError: module 'tkinter' has no attribute 'Tk'
2. tkinter 기본 용법
import tkinter as tk
#
root=tk.Tk()
#
root.title("func")
# Label , 、
theLabel=tk.Label(root,text='this is a funck you')
# Label pack() ,
theLabel.pack()
#
root.mainloop()
Label : , , :
theLabel=tk.Label(root,text='this is a funck you',justify=LEFT,padx=10,image=photo,compound=CENTER,font=(' ',20),fg='white')
root ,text ,justify ,padx ,image ,compound font ,fg ,textvariable
theLabel.pack()
Button : , ,
from tkinter import *
import sys
#
sys.stdout.flush()
root=Tk()
#
frame1=Frame(root)
#
frame2=Frame(root)
def callback():
var.set("funck you")
var=StringVar()
var.set("this is a dangous pig")
textlabel=Label(root,text='dsfdasfdsfds',justify=LEFT)
textlabel.pack(side=LEFT)
photo=PhotoImage(file=r'C:\Users\Administrator\Desktop\106\data\pic\ \k .gif')
imglabel=Label(frame1,image=photo)
imglabel.pack(side=RIGHT)
thebutton=Button(frame2,text='fsdfgdsgdsgwerwr',command=callback)
thebutton.pack()
frame1.pack(padx=10,pady=10)
frame2.pack(padx=10,pady=10)
mainloop()
Checkbutton : ,Radiobutton
from tkinter import *
root=Tk()
#
v=IntVar()
c=Checkbutton(root,text=' ',variable=v)
c.pack()
# , v 1, 0
#
l=Label(root,textvariable=v)
l.pack()
mainloop()
Checkbutton : anchor,N、NE、E、SE、S、SW、NW CENTER
from tkinter import *
root=Tk()
girls=[' ',' ',' ',' ']
v=[]
for girl in girls:
v.append(IntVar())
b=Checkbutton(root,text=girl,variable=v[-1],justify=LEFT)
b.pack(anchor=W)
mainloop()
from tkinter import *
root=Tk()
langs=[("python",1),("python1",2),("python2",3),("python3",4)]
v=IntVar()
v.set(1)
for lang,num in langs:
#indicatoron ,False ,
b=Radiobutton(root,text=lang,variable=v,value=num,indicatoron=False)
b.pack(anchor=W)
mainloop()
LabelFrame : Frame , , label Frame,
from tkinter import *
root=Tk()
group=LabelFrame(root,text=' ?',padx=5,pady=5)
group.pack(padx=10,pady=10)
langs=[("python",1),("python1",2),("python2",3),("python3",4)]
v=IntVar()
v.set(1)
for lang,num in langs:
b=Radiobutton(group,text=lang,variable=v,value=num)
b.pack(anchor=W)
mainloop()
Entry , ,
from tkinter import *
root=Tk()
e=Entry(root)
e.pack(padx=20,pady=20)
e.delete(0,END)
e.insert(0," ")
mainloop()
from tkinter import *
root=Tk()
#tkinter ,pack(),grid() place()
#pack(fill=X) X Y
#grid()
#place() relx rely
#row ,column
Label(root,text=' ').grid(row=0)
Label(root,text=' ').grid(row=1)
v1=StringVar()
v2=StringVar()
e1=Entry(root,textvariable=v1)
e2=Entry(root,textvariable=v2,show='*')
e1.grid(row=0,column=1,padx=10,pady=5)
e2.grid(row=1,column=1,padx=10,pady=5)
def show():
print(" :《%s》"%(e1.get()))
print(" : %s"%(e2.get()))
e1.delete(0,END)
e2.delete(0, END)
# , sticky
Button(root,text=' ',width=10,command=show).grid(row=3,column=0,sticky=W,padx=10,pady=5)
Button(root,text=' ',width=10,command=root.quit).grid(row=3,column=1,sticky=E,padx=10,pady=5)
mainloop()
focus: Entry
focusin Entry
focusout Entry
key
all
none 。 ,none , None
from tkinter import *
root=Tk()
def test():
if e1.get()==' ':
print(" ")
return True
else:
print(" ")
e1.delete(0,END)
return False
#
v=StringVar()
#validate
#validatecommand
e1=Entry(root,textvariable=v,validate='focusout',validatecommand=test)
e2=Entry(root)
e1.pack(padx=10,pady=10)
e2.pack(padx=10,pady=10)
mainloop()
from tkinter import *
root=Tk()
def test():
if e1.get()==' ':
print(" ")
return True
else:
print(" ")
e1.delete(0,END)
return False
def test1():
print(" ")
return True
#
v=StringVar()
#validate
#validatecommand
e1=Entry(root,textvariable=v,validate='focusout',validatecommand=test,invalidcommand=test1)
e2=Entry(root)
e1.pack(padx=10,pady=10)
e2.pack(padx=10,pady=10)
mainloop()
#validatecommand False
, , ,invalidcommand validatecommand False 。
'%d' :0 ;1 ;2 、 textvariable
'%i' , ( ), 、 textvaliable , -1
'%P' , ,
'%s'
'%S' ,
'%v' validate
'%V' , focusin、focusout、key、forced
'%W'
validatecommand=(f,s1,s2,...)
f ,s1,s2,s3 , f , register() .
from tkinter import *
root=Tk()
frame=Frame(root)
frame.pack(padx=10,pady=10)
v1=StringVar()
v2=StringVar()
v3=StringVar()
def test(content):
if content.isdigit():
return True
else:
return False
#register
testCMD=root.register(test)
Entry(frame,textvariable=v1,width=10,validate='key',validatecommand=(testCMD,'%P')).grid(row=0,column=0)
Label(frame,text='+').grid(row=0,column=1)
Entry(frame,textvariable=v2,width=10,validate='key',validatecommand=(testCMD,'%P')).grid(row=0,column=2)
Label(frame,text='=').grid(row=0,column=3)
Entry(frame,textvariable=v3,width=10,validate='key',validatecommand=(testCMD,'%P')).grid(row=0,column=4)
def calc():
result=int(v1.get())+int(v2.get())
v3.set(result)
Button(frame,text=' ',command=calc).grid(row=1,column=2,pady=5)
mainloop()
Listbox : Checkbutton Radiobutton , , Listbox, insert
from tkinter import *
root=Tk()
theLB=Listbox(root,setgrid=True)
theLB.pack()
for item in [' ',' ',' ',' ']:
theLB.insert(END,item)
#ACTIVE ,
#Listbox selectmode 4 ,SINGLE( ),BROWSE( , ),MULTIPLE( ),EXTENDED( , Shift Ctrl), BROWSE
theButton=Button(root,text=' ',command=lambda x=theLB:x.delete(ACTIVE))
theButton.pack()
mainloop()
Scrollbar : ,1、 yscrollbarcommand Scrollbar set() :
2、 Scrollbar command yview()
from tkinter import *
root=Tk()
sb=Scrollbar(root)
sb.pack(side=RIGHT,fill=Y)
lb=Listbox(root,yscrollcommand=sb.set)
for i in range(1000):
lb.insert(END,i)
lb.pack(side=LEFT,fill=BOTH)
sb.config(command=lb.yview)
mainloop()
Scale : , ,
from tkinter import *
root=Tk()
sb=Scrollbar(root)
sb.pack(side=RIGHT,fill=Y)
lb=Listbox(root,yscrollcommand=sb.set)
for i in range(1000):
lb.insert(END,i)
lb.pack(side=LEFT,fill=BOTH)
sb.config(command=lb.yview)
mainloop()
Text : , , . , image window .
from tkinter import *
root=Tk()
text=Text(root,width=30,height=20) #
text.pack()
text.insert(INSERT,"I funck you")
photo=PhotoImage(file=r'C:\Users\Administrator\Desktop\106\data\pic\ \k .png')
def show():
text.image_create(END,image=photo)
b1=Button(text,text=" K ",command=show)
text.window_create(INSERT,window=b1)
mainloop()
Indexes : Text ,Text
1、line.column: , , 1 , 0 . "%d.%d"%(line,column)
, 。 。 。
2、line.end 。
text.insert(INSERT,"I funck you")
print(text.get("1.2","1.end"))
3、INSERT:
4、CURRENT: 。 , 。
5、END: Text
6、user-defined tags: Text 。
"%s.first"%tagname tag
"%s.last"%tagname tag ,
Marks : text 。Marks , 。 INSERT CURRENT user-defined marks
tags : 、 、 、SEL
canvas
from tkinter import *
import webbrowser
root=Tk()
w=Canvas(root,width=200,height=100)
w.pack()
#
line1=w.create_line(0,50,200,50,fill='yellow')
# ( )
line2=w.create_line(100,0,100,100,fill='red',dash=(4,4))
#
rec1=w.create_rectangle(50,25,150,75,fill='blue')
#
w.coords(line1,0,25,200,25)
#
w.itemconfig(rec1,fill='red')
w.delete(line2)
Button(root,text=' ',command=(lambda x=ALL:w.delete(x))).pack()
mainloop()
Canvas , coords、itemconfig()、move() , delete()
w.create_rectangle w.create_oval w.create_text w.create_polygon
w.create_arc 、 、 w.create_bitmap w.create_line w.create_window
Menu组件:实现菜单功能.
from tkinter import *
import webbrowser
root=Tk()
def callback():
print(" ....")
def popup(event):
menu.post(event.x_root,event.y_root)
#
menubar=Menu(root)
# " ",
filemenu=Menu(menubar,tearoff=False)
filemenu.add_command(label=' ',command=callback)
filemenu.add_command(label=' ',command=callback)
filemenu.add_separator()
filemenu.add_command(label=' ',command=root.quit)
menubar.add_cascade(label=' ',menu=filemenu)
editmenu=Menu(menubar,tearoff=False)
editmenu.add_command(label=' ',command=callback)
editmenu.add_command(label=' ',command=callback)
editmenu.add_command(label=' ',command=callback)
menubar.add_cascade(label=' ',menu=editmenu)
root.config(menu=menubar)
frame=Frame(root,width=512,height=512)
frame.pack()
frame.bind("
from tkinter import *
root=Tk()
def callback():
print(" ....")
mb=Menubutton(root,text=' ',relief=RAISED)
mb.pack()
#
menubar=Menu(mb,tearoff=False)
# " ",
filemenu=Menu(menubar,tearoff=False)
filemenu.add_checkbutton(label=' ',command=callback,selectcolor='yellow')
filemenu.add_command(label=' ',command=callback)
filemenu.add_separator()
filemenu.add_command(label=' ',command=root.quit)
mb.config(menu=filemenu)
mainloop()
OptionMebu : , Listbox 。
from tkinter import *
options=['california','458','dfasf','dasd']
root=Tk()
variable=StringVar()
variable.set('you are a bitch')
w=OptionMenu(root, variable,*options)
w.pack()
def callback():
print(variable.get())
Button(root,text=' ',command=callback).pack()
mainloop()
Message : Label , , ,
from tkinter import *
root=Tk()
w1=Message(root,text=' ',width=100)
w1.pack()
w2=Message(root,text=' ',width=100)
w2.pack()
mainloop()
Spinbox : Entry , 。, :Spinbox 。
from tkinter import * root=Tk() w=Spinbox(root,from_=0,to=10) #values
#w=Spinbox(root,values=("傻逼",'逗比','二逼'))w.pack() mainloop() PanedWINDOW : , Frame , 。
from tkinter import *
root=Tk()
m=PanedWindow(orient=VERTICAL)
m.pack(fill=BOTH,expand=1)
top=Label(m,text='top pane')
m.add(top)
bottom=Label(m,text='bottom pane')
m.add(bottom)
mainloop()
Toplevel : Frame , 、 ,attributes() , , 。 : , ,
from tkinter import *
root=Tk()
def create():
top=Toplevel()
top.title("Fishc Demo")
top.attributes('-alpha',0.5)
msg=Message(top,text=" fuck you")
msg.pack()
Button(root,text=' ',command=create).pack()
mainloop()
: 、 , bind() 。 ,Tkinter handler() 。
from tkinter import *
root=Tk()
def callback(event):
print(" :",event.x,event.y)
frame=Frame(root,width=200,height=200)
#bind
frame.bind("
:
type ,
modifier ,
detail ,
H
ctrl+shift+H
-----------------------------------------------------------------------------------------------
type: :
Activate 。
Button 。detail :
(Linux)
ButtonRelease 。
Configure 。
Deactivate 。
Destroy
Enter 。 :
Expose
FocusOut
FocusIn
KeyPress
Leave
Map
Motion
MouseWheel
modifier:
Alt: Alt
Any: 。
Control: Ctrl
Double: 。
Lock (CapsLock)
Shift Shift
Triple Double ,
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.