News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

如何用python做交互式界面?五分钟教会你。

posted on 2023-06-03 20:06     read(476)     comment(0)     like(26)     collect(3)


I wrote an article on the login interface made in Python before. This time, I will teach you to implement a simple registration interface. Compared with the login, the registration interface has added a function of uploading avatars and saving the registration information to a local txt file. The function, the final effect is like this

 

2. Import the necessary libraries

  1. from tkinter import *
  2. # filedialog函数用于上传文件
  3. from tkinter import filedialog
  4. # 因为头像是图片需要用到pillow库
  5. from PIL import Image

3. Create window

After importing the necessary libraries, we first create a window with a width of 350 and a height of 220. The title of the window is called "Registration"
  • Create window - code
  1. # 创建窗口
  2. win = Tk()
  3. # 设置窗口标题
  4. win.title('注册')
  5. # 设置窗口宽度和高度
  6. win.geometry('350x220')
  7. # 主循环
  8. win.mainloop()
  • achieve effect
After completing the above code, the effect of running the program is as follows

 

4. Create account text box

After the window is created, the word "account" will be displayed on the window, and then a text box for inputting the account will be created behind the "account"
  • Create account text box - code
  1. # 将“账号:”显示在x坐标为50, y坐标为100的位置
  2. Label(text='账号:').place(x=50, y=100)
  3. # 用变量uname表示输入账号的文本框,方便获取里面的内容
  4. uname = Entry(win)
  5. # 将用于输入账号的文本框放置在x坐标为100,y坐标为100的位置
  6. uname.place(x=100, y=100)
  • achieve effect

 

5. Create a password text box

Next, use the same method to set the word "Password:" and the text box for entering the password on the window
  • Create password textbox - code
  1. Label(text='密码:').place(x=50, y=140)
  2. # 使用show="*",在文本框输入的内容就全部显示为*
  3. pwd = Entry(win, show='*')
  4. pwd.place(x=100, y=140)
  • achieve effect

6. Click to upload avatar

Next, add a click to upload avatar function in the interface
  • Upload avatar-code
  1. 作者:python爱好者
  2. 链接:https://www.zhihu.com/question/60013978/answer/2222608439
  3. 来源:知乎
  4. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  5. # 创建一个自定义函数,用于点击上传按钮的时候调用
  6. def upload_pic():
  7. # 这行代码会打开C:盘根目录,当你选中文件的时候,将文件的路径保存到pic_path变量
  8. pic_path = filedialog.askopenfilename(initialdir='C:/')
  9. # 根据获取到的图片路径打开图片
  10. img = Image.open(pic_path)
  11. # 将图片固定设置为宽度为80,高度为80方便在界面中展示
  12. img = img.resize((80, 80))
  13. # 想修改完宽和高的图片保存
  14. img.save('head.png')
  15. # 加载保存的图片
  16. head = PhotoImage(file='head.png')
  17. # 将上传按钮中的图片设置为刚才加载的图片
  18. head_button.config(image=head)
  19. head_button.image=head
  20. # 加载一个默认图片,用于在上传按钮中显示
  21. default_pic = PhotoImage(file='default_pic.png')
  22. # 创建一个按钮,设置按钮中要显示的图片,以及点击按钮是调用的函数
  23. head_button = Button(image=default_pic, command=upload_pic)
  24. # 将按钮放置在x坐标为150,y坐标为10的位置,并且设置宽度和高度都是80
  25. head_button.place(x=150, y=10, width=80, height=80)

 

Author: Python lover
Link: https://www.zhihu.com/question/60013978/answer/2222608439
Source: Zhihu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.
 

  • achieve effect

Added a button to upload an avatar in the interface

The effect after selecting a picture to upload

7. Save the registration information to a local txt file

Next, save the entered account, password, and avatar information to a local txt file.
  • save info-code
  1. # 创建register函数,用于在点击注册按钮的时候调用
  2. def register():
  3. # 从输入账号的文本框中获取账号
  4. user_name = uname.get()
  5. # 从输入密码的文本框中获取密码
  6. password = pwd.get()
  7. # 保存上传的图片路径
  8. head_path = 'head.png'
  9. # 将以上三条信息写入到本地的txt文件
  10. with open('data.txt', 'a', encoding='utf-8') as f:
  11. f.write('{},{},{}\n'.format(user_name,password,head_path))
  12. # 创建注册按钮,点击按钮时调用register函数
  13. Button(text='注册', command=register).place(x=100, y=180, width=190)
  • achieve effect

Added a registration interface to the interface

After the avatar, account number, and password are ready, click the registration button, and the information will be saved in the "data.txt" file

8 complete code

  1. 作者:python爱好者
  2. 链接:https://www.zhihu.com/question/60013978/answer/2222608439
  3. 来源:知乎
  4. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  5. from tkinter import *
  6. from tkinter import filedialog
  7. from PIL import Image
  8. # 注册窗口
  9. win = Tk()
  10. win.title('注册')
  11. win.geometry('350x220')
  12. win.resizable(0, 0)
  13. # 账号文本框
  14. Label(text='账号:').place(x=50, y=100)
  15. uname = Entry(win)
  16. uname.place(x=100, y=100)
  17. # 密码文本框
  18. Label(text='密码:').place(x=50, y=140)
  19. pwd = Entry(win, show='*')
  20. pwd.place(x=100, y=140)
  21. # 上传头像
  22. def upload_pic():
  23. pic_path = filedialog.askopenfilename(initialdir='C:/')
  24. img = Image.open(pic_path)
  25. img = img.resize((80, 80))
  26. img.save('head.png')
  27. head = PhotoImage(file='head.png')
  28. head_button.config(image=head)
  29. head_button.image=head
  30. default_pic = PhotoImage(file='default_pic.png')
  31. head_button = Button(image=default_pic, command=upload_pic)
  32. head_button.place(x=150, y=10, width=80, height=80)
  33. # 注册
  34. def register():
  35. user_name = uname.get()
  36. password = pwd.get()
  37. head_path = 'head.png'
  38. with open('data.txt', 'a', encoding='utf-8') as f:
  39. f.write('{},{},{}\n'.format(user_name,password,head_path))
  40. Button(text='注册', command=register).place(x=100, y=180, width=190)
  41. win.mainloop()

Q&A source code sharing can pay attention to the official account: Python source code acquisition



Category of website: technical article > Blog

Author:Disheartened

link:http://www.pythonblackhole.com/blog/article/78459/050523fc3b5e0935bfdd/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

26 0
collect article
collected

Comment content: (supports up to 255 characters)