首页 > 编程语言 > 详细

Python GUI之 Tkinter -2

时间:2021-02-01 21:40:21      阅读:49      评论:0      收藏:0      [点我收藏+]

grid布局1

网格布局 Label(tk,text=‘daodantou‘,width=15,height=3,bg=‘gold‘).grid(row=0,column=0)

row默认递增 column默认0 行号、列号    sticky有空间时 对齐方式N E S W NW NE SW SE

rowspan 所跨行数  columnspan 所跨列数   ipadx ipady padx pady 内边距 外边距

小控件放置单元格一角 ‘ne’ ‘se’ ‘sw’ ‘nw’ 拉伸小控件 ‘ns’ ‘ew’ ‘nsew’ ‘nsw’

tk.grid_columnconfigure(0,weight=1)  实现拉伸窗口同步拉伸原件 搭配sticky=’ew’效果更好

from tkinter import *

tk=Tk()

tk.title(‘long time ago‘)

tk.geometry(‘565x220‘)

tk.iconbitmap(‘plus.ico‘)

Label(tk,text=‘Find:‘).grid(row=0,column=0,sticky=‘e‘)

Entry(tk,text=‘Find‘,width=60).grid(row=0,column=1,columnspan=3,sticky=‘ew‘)

Button(tk,text=‘Find‘).grid(row=0,column=4,sticky=‘ew‘,padx=3,pady=6)

Label(tk,text=‘Replace:‘).grid(row=1,column=0)

Entry(tk,text=‘Find‘,width=60).grid(row=1,column=1,columnspan=3,sticky=‘ew‘)

Button(tk,text=‘Find All‘).grid(row=1,column=4,sticky=‘ew‘,padx=3,pady=6)

Checkbutton(tk,text=‘Match whole word only‘).grid(row=2,column=1,sticky=‘w‘)

Label(tk,text=‘Direction‘).grid(row=2,column=2,columnspan=2,sticky=‘w‘)

Button(tk,text=‘Replace‘).grid(row=2,column=4,sticky=‘ew‘,padx=3,pady=6)

Checkbutton(tk,text=‘Match Case‘).grid(row=3,column=1,sticky=‘w‘)

Radiobutton(tk,text=‘UP‘,value=1).grid(row=3,column=2,sticky=‘w‘)

Radiobutton(tk,text=‘DOWN‘,value=2).grid(row=3,column=3,sticky=‘w‘)

Button(tk,text=‘Replace All‘).grid(row=3,column=4,sticky=‘ew‘,padx=3,pady=6)

Checkbutton(tk,text=‘Wrap around‘).grid(row=4,column=1,columnspan=4,sticky=‘w‘)

tk.grid_columnconfigure(1,weight=1)

tk.mainloop()

place布局

anchor 默认nw  x,y 左上角坐标   relx,rely 相对父容器坐标 width、height宽度高度

relwidth、relheight 相对父容器宽高 bordermode的值INSIDE  OUTSIDE外部大小相对

最有用、用的最多的是relx和rely  可实现叠加效果!

布局实战

技术分享图片

grid实现代码

from tkinter import *

from PIL import Image,ImageTk

tk=Tk()

tk.title(‘gold come here‘)

Label(tk,text=‘账号:‘).grid(row=0,column=0,sticky=‘e‘)

Entry(tk).grid(row=0,column=1,sticky=‘ew‘)

img=Image.open(‘lo.jpg‘)

photo=ImageTk.PhotoImage(img)

Label(tk,image=photo).grid(row=0,rowspan=2,column=2,columnspan=2)

Label(tk,text=‘密码:‘).grid(row=1,column=0,sticky=‘e‘)

Entry(tk).grid(row=1,column=1,sticky=‘ew‘)

Checkbutton(tk,text=‘男‘).grid(row=2,column=0,columnspan=2,sticky=‘ew‘)

Button(tk,text=‘登录‘).grid(row=2,column=2,sticky=‘ew‘)

Button(tk,text=‘退出‘).grid(row=2,column=3,sticky=‘ew‘)

tk.mainloop()

事件机制1 利用了command 主要是button和小部分控件有这个,并且局限单击和空格触发。

添加命令,无参数

添加命令,有参数

from tkinter import *

tk=Tk()

tk.title(‘long time ago‘)

tk.geometry(‘400x200‘)

def callback():

    print(‘me is clicked‘)

Button(tk,text=‘发送‘,command=callback).pack()

tk.mainloop()

from tkinter import *

tk=Tk()

def callback(x):

    print(‘被点击了‘,x.get())

val=StringVar()

Entry(tk,textvariable=val).pack()

Button(tk,text=‘发送‘,command=lambda:callback(val)).pack()

tk.mainloop()

事件机制2

事件与绑定

鼠标事件 5个操作

<Button-1>:鼠标左击事件

<Button-2>:鼠标滚动事件

<Button-3>:鼠标右击事件

<Double-Button-1>:鼠标左双击事件

<Triple-Button-1>:鼠标左三击事件

from tkinter import *

tk=Tk()

tk.title(‘long‘)

def callback(event):

    print(‘EventType=‘,event.type)

    print(‘Num=‘,event.num)

frame=Frame(tk,bg=‘pink‘,width=100,height=80)

frame.bind(‘<Button-1>‘,callback)

frame.pack()

tk.mainloop()

char 键盘事件,按键的字符

delta 鼠标滚动事件,鼠标滚动距离

height width 仅用于Configure事件,当控件形状变化后的宽和高,相当于SizeChanged事件

keycode 键盘事件,按键码

keysym,keysym_num 按键事件

num 鼠标事件,按键码 1为左键 2为中键 3为右键

serial 表示修饰键状态,即ctrl shift alt等修饰键的状态

time 事件发生的事件

type 事件的类型

widget 事件的源控件

x y x_root y_root 鼠标事件中鼠标的位置 x_root为绝对坐标,x为相对坐标

事件传参

from tkinter import *

tk=Tk()

def callback(event,a,b):

    print(a,b)

    print(‘EventType=‘,event.type)

frame=Frame(tk,bg=‘pink‘,width=100,height=100)

frame.bind(‘<Button-1>‘,lambda event:callback(event,1,3))

frame.pack()

tk.mainloop()

键盘事件

from tkinter import *

tk=Tk()

Label(tk,text=‘按键事件触发‘).pack()

def callback(event):

    print(‘EventType=‘,event.type)

    print(‘keysym=‘,event.keysym)

frame=Frame(tk,bg=‘gold‘,width=100,height=100)

frame.pack()

tk.bind(‘<KeyPress-a>‘,callback)

tk.bind(‘<KeyPress-F1>‘,callback)

tk.bind(‘<Control-Alt-j>‘,callback)

tk.mainloop()

结果:

EventType= KeyPress

keysym= a

EventType= KeyPress

keysym= F1

EventType= KeyPress

keysym= j

绑定级别 三种绑定级别 实际开发选择合适一种

1 实例绑定

frame.bind(‘<Button-1>’,callback)

2 类级绑定 慎用 不可预知问题

my_entry.bind_class(‘Entry’,’<Control-V>’,paste)

3 应用程序级绑定

root.bind_all(‘<F1>’,show_help)

取消绑定

entry.unbind(‘<Alt-Shift-5>’)

root.unbind_class(‘Entry’,’<KeyPress-Del>’)

root.unbind_all(‘<F1>’)

手动模拟事件 代码触发事件 而不是共同过事件源产生事件

tkinter的所有widget都包含一个公共方法 event_generate 用来产生相应事件

event_generate(‘<<Copy>>’)

文档查询地址https://www.tcl.tk/man/tcl8.7/TkCmd/contents.htm

from tkinter import *

def paste(editor,event=None):

    editor.event_generate(‘<<Paste>>‘)

#用于通过回调函数设置

root=Tk()

e1=Entry(root,width=30)

e1.pack()

#也可以直接作为方法使用

e2=Entry(root,width=30)

e2.pack()

e2.update()

e2.event_generate(‘<<Paste>>‘)  #自发触发

#可能与焦点无关

e3=Entry(root,width=30)

e3.pack()

e3.focus_set()  #设置焦点

Button(root,text="粘贴",command=lambda:paste(e1)).pack()

root.mainloop()

面向对象范式

聚合式

from tkinter import *

class App:

    def __init__(self,root):

        self.root=root

        self.set_window()

        self.create_top()

        self.create_body()

        self.create_bottom()

    def set_window(self):

        self.root.title(‘long‘)

        self.root.resizable(0,0)

    def create_top(self):

        Label(self.root,text=‘Top‘).pack()

    def create_body(self):

        self.input=StringVar()

        Entry(self.root,textvariable=self.input).pack()

    def create_bottom(self):

        Button(self.root,text=‘Bottom‘,command=self.onclick).pack()

    def onclick(self):

        print(self.input.get())

if __name__ == ‘__main__‘:

    root=Tk()

    App(root)

    root.mainloop()

继承式

from tkinter import *

class App(Tk):

    def __init__(self):

        super().__init__()

        self.set_window()

        self.create_top()

        self.create_body()

        self.create_bottom()

    def set_window(self):

        self.title(‘long‘)

        self.resizable(0,0)

    def create_top(self):

        Label(self,text=‘Top‘).pack()

    def create_body(self):

        self.input=StringVar()

        Entry(self,textvariable=self.input).pack()

    def create_bottom(self):

        Button(self,text=‘Bottom‘,command=self.onclick).pack()

    def onclick(self):

        print(self.input.get())

if __name__ == ‘__main__‘:

    app=App()

    app.mainloop()

第三章 高级用法

大部分控件共享属性

background(bg)   背景颜色RGB

borderwidth(bd)  边界宽度

cursor           鼠标样式

font            文本字体

foreground(fg)    前景色

highlightbackground 高亮

highlightcolor      高亮

highlightthickness   区域加亮

relief      3D效果 RAISED GROOVE

takefocus  boolean是否焦点

width    宽度

多控件共享属性

activebackground  颜色

activeforeground  颜色

anchor          N NE E SE S SW W NW CENTER 9个

bitmap          位图

command        命令关联

disabledforeground  颜色

height           高度

image            图片

justify         LEFT CENTER RIGHT  多行文本对齐

padx  pady

selectbackground   selectborderwidth   selectforeground

浏览器输入ip.gs  或 命令窗口curl ip.gs 可查询公网IP

Python GUI之 Tkinter -2

原文:https://www.cnblogs.com/tankercode/p/14358054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!