python视窗编程开始按钮_【莫烦Python】Tkinter 做简单的窗口视窗<学习笔记(2)>...

import tkinter as tk

import pickle

import tkinter.messagebox

window=tk.Tk()

window.title('Welcome to Mofan Python')

window.geometry('450x300')

canvas=tk.Canvas(window,height=200,width=500)

image_file=tk.PhotoImage(file='C:/Users/Administrator/Desktop/welcome.gif')

image=canvas.create_image(0,0,anchor='nw',image=image_file)

canvas.pack(side='top')

#user information

tk.Label(window,text='User name:').place(x=50,y=150)#此处的x和y代表的是像素点的意思

tk.Label(window,text='Password:').place(x=50,y=190)

#entry

var_usr_name=tk.StringVar()

var_usr_name.set('example@python.com')

entry_user_name=tk.Entry(window,textvariable=var_usr_name)

entry_user_name.place(x=160,y=150)

var_usr_pwd=tk.StringVar()

entry_user_pwd=tk.Entry(window,textvariable=var_usr_pwd,show='*')

entry_user_pwd.place(x=160,y=190)

#entry

def usr_login():

#首先获得entry中输入的东西

usr_name=var_usr_name.get()

usr_pwd = var_usr_pwd.get()

try:#pickle 提取或输入用户信息

with open('usrs_info.pickle','rb') as usr_file:

usrs_info=pickle.load(usr_file)

except FileNotFoundError:

with open('usrs_info.pickle','wb') as usr_file:

usrs_info={'admin':'admin'}#设置管理员用户信息

pickle.dump(usrs_info,usr_file)#把管理员的用户信息传进去

if usr_name in usrs_info:

if usr_pwd == usrs_info[usr_name]:

tk.messagebox.showinfo(title='Welcome',message='How are you?'+usr_name)

else:

tk.messagebox.showerror(message='Error,you password is wrong,try again.')

else:

is_sign_up=tk.messagebox.askyesno(title='Welcome',message='you have not sign up yet.Sign up today?')

if is_sign_up:

usr_sign_up()

#定义sign_up窗口

def usr_sign_up():#Toplevel窗口上的窗口

def sign_to_Mofan_Python():

#把所有输入的信息保存起来

np=new_pwd.get()

npf=new_pwd_confirm.get()

nn=new_name.get()

#确认有没有重复注册过

with open('usrs_info.pickle','rb') as usr_file:

exist_usr_info=pickle.load(usr_file)

if np!=npf:

tk.messagebox.showerror('Error','Password and confirm password must be the same!')

elif nn in exist_usr_info:

tk.messagebox.showerror('Error','The user has already signed up!')

else:#注册合格,将注册的信息存进database中,并pickle起来

exist_usr_info[nn]=np

with open('usrs_info.pickle','wb') as usr_file:

pickle.dump(exist_usr_info,usr_file)

tk.messagebox.showinfo('Welcome','You have successfully signed up!')

window_sign_up.destroy()#摧毁窗口

window_sign_up=tk.Toplevel(window)

window_sign_up.geometry('350x200')

window_sign_up.title('Sign up window')

new_name=tk.StringVar()

new_name.set('example@python.com')

tk.Label(window_sign_up,text='User name:').place(x=10,y=10)

tk.Entry(window_sign_up,textvariable=new_name).place(x=150,y=10)

new_pwd=tk.StringVar()

tk.Label(window_sign_up,text='Password:').place(x=10,y=50)

entry_user_pwd=tk.Entry(window_sign_up,textvariable=new_pwd,show='*')

entry_user_pwd.place(x=150,y=50)

new_pwd_confirm = tk.StringVar()

tk.Label(window_sign_up,text='Confirm password:').place(x=10,y=90)

entry_user_pwd_confirm=tk.Entry(window_sign_up,textvariable=new_pwd_confirm,show='*')

entry_user_pwd_confirm.place(x=150,y=90)

btn_comfirm_sign_up=tk.Button(window_sign_up,text='Sign up',command=sign_to_Mofan_Python)

btn_comfirm_sign_up.place(x=150,y=130)

btn_login=tk.Button(window,text='Login',command=usr_login)

btn_login.place(x=170,y=230)

btn_login=tk.Button(window,text='Sign up',command=usr_sign_up)

btn_login.place(x=270,y=230)

window.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来回答你的问题。以下是一个使用 Tkinter 和 FileDialog 实现打开和保存文件的示例程序: ```python import tkinter as tk from tkinter import filedialog class App(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.pack() self.create_widgets() def create_widgets(self): self.text = tk.Text(self) self.text.pack() self.open_button = tk.Button(self, text="打开文件", command=self.open_file) self.open_button.pack(side="left") self.save_button = tk.Button(self, text="保存文件", command=self.save_file) self.save_button.pack(side="left") def open_file(self): file_path = filedialog.askopenfilename() if file_path: with open(file_path, "r") as file: file_content = file.read() self.text.delete("1.0", tk.END) self.text.insert(tk.END, file_content) def save_file(self): file_path = filedialog.asksaveasfilename(defaultextension=".txt") if file_path: with open(file_path, "w") as file: file_content = self.text.get("1.0", tk.END) file.write(file_content) root = tk.Tk() app = App(master=root) app.mainloop() ``` 这个程序创建了一个简单的文本编辑器,包含打开文件和保存文件的按钮。当用户点击“打开文件”按钮时,程序会弹出一个文件对话框,用户可以选择要打开的文件。当用户点击“保存文件”按钮时,程序会弹出一个文件对话框,用户可以选择保存文件的路径和文件名。程序会将用户输入的内容保存到文件中。 希望这个示例对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值