python图形界⾯编程(GUI)
1. Tkinter
在开始GUI编程之前,需要先了解这⼏个概念:窗⼝和控件、事件驱动处理、布局管理器。
窗体控件: 窗体、标签、按钮、列表框、滚动条等。
事件驱动:按下按钮及释放、⿏标移动、按回车键等。
布局管理:Tk有3种布局管理器:Placer、Packer、Grid
1.1 窗体控件
tkinter提供各种控件,如按钮、标签和⽂本框等,在⼀个GUI应⽤程序中使⽤。这些控件有时也被称为部件。⽬前有19种tkinter的控件。
1.1.1 ⽣成根窗体
from tkinter import *
top = Tk()
top.mainloop()
1.1.2 添加标签
from tkinter import *
top = Tk()
label = Label(top, text = "hello tkinter")
label.pack()
top.mainloop()
1.1.3 添加按钮
from tkinter import *
top = Tk()
label = Label(top, text = "hello tkinter")
button = Button(top ,text="第⼀个按钮")
label.pack()
button.pack()
top.mainloop()
1.1.4 Checkbutton控件
from tkinter import *
top = Tk()
check_music = IntVar()
check_video = IntVar()
label = Label(top, text = "hello tkinter")
button = Button(top ,text="第⼀个按钮")
check_m = Checkbutton(top,text="Music", variable = check_music, onvalue =1, offvalue =0)
check_v = Checkbutton(top,text="Video", variable = check_video, onvalue =1, offvalue =0) label.pack()
button.pack()
check_m.pack()
check_v.pack()
top.mainloop()
1.1.5 Text控件
from tkinter import *
top = Tk()
check_music = IntVar()
check_video = IntVar()
label = Label(top, text = "hello tkinter")
button = Button(top ,text="第⼀个按钮")
check_m = Checkbutton(top,text="Music", variable = check_music, onvalue =1, offvalue =0) check_v = Checkbutton(top,text="Video", variable = check_video, onvalue =1, offvalue =0) font = ("⿊体", 10, "bold")
text = Text(top,height=5, width=30, font=font, bg="white", fg="black")
text.insert(INSERT,"Hello GUI,")
text.insert(END, "Bye!")
label.pack()
button.pack()
check_m.pack()
check_v.pack()
text.pack()
top.mainloop()
1.1.6 Menu控件
from tkinter import *
win = Tk()
win.title("Python GUI")
qt进程间通信def _quit():
win.quit()
win.destroy()
exit()
# 创建菜单栏功能
menuBar = Menu(win)
font = ("⿊体", 10, "bold")
fileMenu = Menu(menuBar, tearoff=0, font=font, bg="white", fg="black")
menuBar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(labe="New File")
fileMenu.add_separator()
fileMenu.add_command(labe="Exit",command=_quit)
helpMenu = Menu(menuBar, tearoff=0, font=font, bg="white", fg="black")
menuBar.add_cascade(label="Help", menu=helpMenu)
helpMenu.add_command(labe="About")
check_music = IntVar()
check_video = IntVar()
label = Label(win, text = "hello tkinter")
button = Button(win ,text="第⼀个按钮")
check_m = Checkbutton(win,text="Music", variable = check_music, onvalue =1, offvalue =0) check_v = Checkbutton(win,text="Video", variable = check_video, onvalue =1, offvalue =0) text = Text(win,height=5, width=30, font=font, bg="white", fg="black")
text.insert(INSERT,"Hello GUI,")
text.insert(END, "Bye!")
label.pack()
button.pack()
check_m.pack()
check_v.pack()
text.pack()
win .mainloop()
1.2 实际应⽤
制作TCP通信的Server 和 Client
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 15:34:10 2020
@author: sinlearn
"""
import tkinter as tk
k as ttk
import socket
import threading
import time
class TCP_Server():
def __init__(self):
winserver = tk.Tk()
winserver.title("TCP Server")
font = ("宋体", 10)
self.rbtn = tk.Radiobutton(winserver, text="未连接", fg="red")
self.label_port = tk.Label(winserver, text=" 端⼝:", font=font)
self.label_send = tk.Label(winserver, text=" 发送区:", font=font)
self.label_recv = tk.Label(winserver, text=" 接收区:", font=font)
self.label_clist = tk.Label(winserver, text=" 客户端列表:", font=font)
self.spinbox_port = tk.Spinbox(winserver, from_=1024, to=10000)
self.btn_start = tk.Button(winserver, text="启动", bg="white", command=self.do_start)
self.btn_stop = tk.Button(winserver, text="停⽌", bg="white", command=self.do_stop)
self.btn_send = tk.Button(winserver, text="发送", bg="white", command=self.send_to_client)        _send = tk.Entry(winserver, text="Test", bd=2)
<_recv = tk.Text(winserver, height=5, width=43, font=font, bg="white", fg="black")
self.client_list = ttk.Treeview(winserver, height=10, show="headings",
columns=('col1', 'col2', 'col3'))  # show = "headings" 隐藏默认的col0列        self.lumn('col1', width=50, anchor='center')
self.lumn('col2', width=200, anchor='center')
self.lumn('col3', width=100, anchor='center')
self.client_list.heading('col1', text='序号')
self.client_list.heading('col2', text='IP地址')
self.client_list.heading('col3', text='端⼝号')
self.rbtn.place(x=10, y=10)
self.label_port.place(x=100, y=15)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。