Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。
由于 Tkinter 是内置到 python 的安装包中、只要安装好 Python 之后就能 import Tkinter 库,对简单图形界面的实现十分简单。
在引入"import tkinter"后即可使用,以下两行即可运行出窗口,
loop = tkinter.Tk() # 定义tk的实例 loop.mainloop() # 运行窗口
但我们的需求肯定不止如此,通过使用tkinter自带的方法才能创造实际可用的窗口。
以下方法仅能规定窗口的大小的因素。
loop.title('窗口名') # 定义窗口的窗口名 loop.geometry('300x120+600+210') # 300x120定义窗口的大小 #+600+210定义窗口的位置 loop.resizable(width=False, height=False) # 不可修改窗口大小
窗口的内容需要使用控件进行绑定,如:
btn=Button(master=root, width=6, height=2) # 意为定义一个按钮在root窗口中,宽为6,高为2
当然button还有许多其它的属性,如:
command 按钮关联的函数,当按钮被点击时,执行该函数;text 定义按钮的文本内容
下面是一个简易聊天客户室客户端代码,忽略掉socket和threading的内容即可:
import tkinter from tkinter import * import socket import threading import tkinter.messagebox from tkinter import ttk def recv(sock, addr): sock.sendto(''.encode('utf-8'), addr) while True: data = sock.recv(1024) Data = data.decode('utf-8') print(Data) if 'ACCCSXOWSASDC' in Data: message.insert('end', '欢迎...' + '\n') elif '私聊' == Data: data_1 = sock.recv(1024) message.insert('end', 'if 私聊 ' + data_1.decode('utf-8')) message.insert('end', '\n') # elif '在线用户' == data.decode('utf-8'): # data_2 = sock.recv(1024) # message.insert('end', 'if 在线用户 == data.decode(utf-8):' + data_2.decode('utf-8')) # message.insert('end', '\n') else: message.insert('end', data.decode('utf-8')) message.insert('end', '\n') def enter(): msg = text.get('0.0', END)[:-1] # msg自带一个'\n' text.delete('0.0', END) send(s, address, msg) def send(sock, addr, msg): # if msg == '注销': # sock.sendto('注销'.encode('utf-8'), addr) # message.insert('end', '\n') # message.insert('end', '链接已断开') # else: data = msg.encode('utf-8') sock.sendto(data, addr) def socketConnect(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) address = ('127.0.0.1', 10086) s.connect(address) return s, address def outLogin(): # 注销登录函数 s.sendto('2023#05&25('.encode('utf-8'), address) # 为减少发送的信息引起误注销,采用密钥形式 message.insert('end', '\n') message.insert('end', '链接已断开') loop.destroy() def rename(msg): s.sendto('《2023、05)2'.encode('utf-8'), address) # 为减少发送的信息引起误注销,采用密钥形式 s.sendto(msg.encode('utf-8'), address) def repassword(msg): s.sendto('《20asdwc)2'.encode('utf-8'), address) # 为减少发送的信息引起误注销,采用密钥形式 s.sendto(msg.encode('utf-8'), address) def ReName(): # 重命名窗口 re = Tk() re.title('重命名') re.geometry('300x120+600+210') re.resizable(0, 0) # 设置账号 Label(re, text='新昵称:').place(x=50, y=30) reame = Entry(re) reame.place(x=100, y=30) # 设置密码 Button(re, text='确定', command=lambda: rename(reame.get()), ).place(x=140, y=60) # 登陆按钮 re.mainloop() # s.sendto('重命名'.encode('utf-8'), address) # enter() def RePassword(): # 重命名窗口 re = Tk() re.title('修改密码') re.geometry('300x120+600+210') re.resizable(0, 0) # 设置账号 Label(re, text='新密码:').place(x=50, y=30) reame = Entry(re) reame.place(x=100, y=30) # 设置密码 Button(re, text='确定', command=lambda: repassword(reame.get()), ).place(x=140, y=60) # 登陆按钮 re.mainloop() def select(fw): global Forward Forward = fw s.sendto('SCSDC@@$A'.encode('utf-8'), address) s.sendto(Forward.encode('utf-8'), address) message.insert('end', '\n') message.insert('end', '\n') message.insert('end', '_______________ ' +Forward+' _________________') message.insert('end', '\n') def shift(): s.sendto('#^ACSO()'.encode('utf-8'), address) data = s.recv(1024) AddressList = data.decode('utf-8') AddressList = AddressList ButtonList = AddressList.split(' ') # 至多显示九个按钮 ValueList = AddressList.split(' ') root = Tk() root.title('选择对象') root.geometry('115x600+400+0') sx = 20 for i in range(0, len(ButtonList)): ButtonList[i] = Button(master=root, width=6, height=2, text=ValueList[i], command=lambda value=ValueList[i]: select(value)) ButtonList[i].place(x=40, y=sx) sx += 60 root.mainloop() s, address = socketConnect() pro = 'unsafe' win = Tk() win.title('登陆') win.geometry('300x150+600+210') win.resizable(0, 0) # 设置账号 Label(text='账号:').place(x=50, y=30) uname = Entry(win) uname.place(x=100, y=30) # 设置密码 Label(text='密码:').place(x=50, y=70) pwd = Entry(win, show='*') pwd.place(x=100, y=70) # 登陆判断 def login(): idd = uname.get() password = pwd.get() msg = idd + '/' + password try: if len(idd) < 8 or len(password) < 8: tkinter.messagebox.showinfo(title='worry', message='账号密码都不少于8位') else: send(s, address, msg) data = s.recv(1024) if '登录成功' in data.decode('utf-8'): global pro pro = 'safe' win.destroy() else: tkinter.messagebox.showinfo(title='worry', message='登录失败') except: tkinter.messagebox.showinfo(title='worry', message='服务器暂未开启') def register(): idd = uname.get() password = pwd.get() msg = idd + '/' + password + '/' + '佚名' try: if len(idd) < 8 or len(password) < 8: tkinter.messagebox.showinfo(title='worry', message='账号密码都不少于8位') else: send(s, address, 'R2023$05S25') send(s, address, msg) data = s.recv(1024) if '注册成功' in data.decode('utf-8'): tkinter.messagebox.showinfo(title='worry', message=data.decode('utf-8') + '(登录后修改)') else: tkinter.messagebox.showinfo(title='worry', message='注册失败') except: tkinter.messagebox.showinfo(title='worry', message='服务器暂未开启') Button(text='登陆', command=login).place(x=100, y=110) # 登陆按钮 Button(text='注册', command=register).place(x=180, y=110) # 注册按钮 win.mainloop() global Forward Forward = 'all' # 聊天对象为全体 loop = Tk() loop.geometry("640x525+480+100") loop.title('简易聊天室') loop.resizable(width=False, height=False) # 不可修改窗口大小 messageFrame = Frame(width=480, height=350, bg='white') TopSendFrame = Frame(width=60, height=350, bg='silver') DownSendFrame = Frame(width=60, height=150, bg='silver') textFrame = Frame(width=480, height=150, bg='gray', padx=3, pady=3) message = Text(messageFrame) text = Text(textFrame) btn1 = Button(DownSendFrame, text='发送信息', command=enter) btn2 = Button(TopSendFrame, text='注销账号', command=outLogin) btn3 = Button(TopSendFrame, text='编辑昵称', command=ReName) btn4 = Button(TopSendFrame, text='切换聊天', command=shift) btn5 = Button(TopSendFrame, text='修改密码', command=RePassword) messageFrame.grid(row=0, column=0) textFrame.grid(row=1, column=0) TopSendFrame.grid(row=0, column=1) DownSendFrame.grid(row=1, column=1) messageFrame.grid_propagate(0) textFrame.grid_propagate(0) TopSendFrame.grid_propagate(0) DownSendFrame.grid_propagate(0) tr = threading.Thread(target=recv, args=(s, address), daemon=True) tr.start() message.grid() text.grid() btn1.grid() btn4.grid() btn3.grid() btn5.grid() btn2.grid() if 'safe' == pro: loop.mainloop()
标签:tkinter,text,re,初见,address,utf,message,4.30,data From: https://www.cnblogs.com/xxaxf/p/17471175.html