python之GUI編程-tkinter學習


推薦幾個學習網址:
https://www.cnblogs.com/shwee/p/9427975.html

https://cloud.tencent.com/developer/section/1372347#stage-100040129

https://docs.python.org/3/library/tkinter.html

一、源代碼

 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()

 

二、運行結果:

三、打包exe

  1.安裝pyinstaller    

  pip install pyinstaller

  2.打包python文件

  pyinstaller -F -w xxx.py

  注:使用-F會在當前目錄下的dist文件夾生成一個獨立的exe文件,而使用-D(默認選項)則會在當前目錄下的dist文件夾生成一個文件夾存放exe以及所有依賴文件。當py文件有窗體時使用-w,在windows下不會顯示控制台,-w在*NIX系統里會被忽略。

  參照:https://www.cnblogs.com/pcat/p/8463939.html

四、打包后的exe,下載點擊可直接 運行(python>打包程序>RBoot.exe)

鏈接:https://pan.baidu.com/s/1DqjnZVTgBPSER6v5uGI6mQ 
提取碼:0kz3 
注:若鏈接失效請留言,單獨發你哦!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM