首页 > 编程语言 > 详细

python之GUI编程-tkinter学习

时间:2019-09-26 09:11:31      阅读:106      评论:0      收藏:0      [点我收藏+]

新手刚学习几天,勿喷!好多东西不会,没有相关文档全面学习,有资源的希望分享下!总的来说很难用。

一、源代码

 1 import itchat
 2 import requests
 3 import tkinter
 4 from tkinter import messagebox
 5 
 6 
 7 def func(self):
 8     cont = text2.get(1.0, end).replace(\n, ‘‘)
 9     if len(cont) == 0:
10         messagebox.showinfo("错误", "不能发送空消息")
11         return
12     else:
13         try:
14             req = requests.get(http://i.itpk.cn/api.php, params={question: cont})
15             text1.config(state=normal)
16             text1.insert(tkinter.INSERT, 我:  + cont + \n)
17             text1.config(state=disabled)
18             text2.delete(1.0, end)
19             text1.config(state=normal)
20             text1.insert(tkinter.INSERT, 夏莉:  + req.text + \n)
21             text1.config(state=disabled)
22         except:
23             messagebox.showinfo("错误", "发送失败")
24             return
25 
26 
27 def enterkeyfunc(event):
28     if event.keycode == 13:
29         func(event)
30 
31 
32 def mouseenterfunc(event):
33     button1[bg] = green
34 
35 
36 def mouseleavefunc(event):
37     button1[bg] = white
38 
39 
40 win = tkinter.Tk()
41 win.title("机器人夏莉")
42 # 宽度x高度+距离屏幕横向400+纵向100
43 win.geometry("400x500+400+100")
44 # 禁止最大化
45 win.wm_resizable(False, False)
46 
47 text1 = tkinter.Text(win, height=26, bg=#e6f1f1)
48 text2 = tkinter.Text(win, height=9, bg=white)
49 button1 = tkinter.Button(win, text="发送(S)", width=8, height=1)
50 
51 text1.pack(anchor=tkinter.N)
52 text2.pack(anchor=tkinter.CENTER)
53 button1.pack(anchor=tkinter.SE)
54 text1.config(state=disabled)
55 
56 text2.focus()
57 
58 # 绑定事件
59 button1.bind("<Button-1>", func)
60 button1.bind("<Enter>", mouseenterfunc)
61 button1.bind("<Leave>", mouseleavefunc)
62 text2.bind("<Key>", enterkeyfunc)
63 
64 win.mainloop()

 

二、运行结果:

技术分享图片

 

python之GUI编程-tkinter学习

原文:https://www.cnblogs.com/zhuanjiao/p/11588346.html

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