class LoginFrame(tk.Frame): def init(self, master=None): super().init(master) self.master = master self.master.title('登录系统') self.master.geometry('300x200') # 读取图片并创建 Label 组件 image = Image.open('C:/Users/hp/Desktop/orginalmodel/orginalmodel/pic1.jpg') image = image.resize((70,70)) photo = ImageTk.PhotoImage(image) self.image_label = tk.Label(self.master, image=photo) self.image_label.image = photo # 防止图片被垃圾回收器回收 self.image_label.pack() self.username_frame = tk.Frame(self.master) self.username_frame.pack() self.username_label = tk.Label(self.username_frame, text='用户名 ') self.username_label.pack(side=tk.LEFT) self.username_entry = tk.Entry(self.username_frame) self.username_entry.pack(side=tk.LEFT) self.password_frame = tk.Frame(self.master) self.password_frame.pack() self.password_label = tk.Label(self.password_frame, text=' 密码 ') self.password_label.pack(side=tk.LEFT) self.password_entry = tk.Entry(self.password_frame, show='*') self.password_entry.pack(side=tk.LEFT) self.login_button = tk.Button(self.master, text='登录', command=self.login) self.login_button.pack() def login(self): username = self.username_entry.get() password = self.password_entry.get() if username == 'AAA' and password == '111': self.master.destroy() root = tk.Tk() app = MainFrame(root) app.mainloop() else: tk.messagebox.showerror('错误', '用户名或密码错误!')修改代码,使pic1.jpg作为界面背景图
时间: 2023-07-14 09:11:50 浏览: 105
可以使用`tkinter`的`Canvas`组件来实现界面背景图片的设置。以下是修改后的代码:
```python
class LoginFrame(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title('登录系统')
self.master.geometry('300x200')
# 创建 Canvas 组件并添加背景图片
self.canvas = tk.Canvas(self.master, width=300, height=200)
self.canvas.pack()
image = Image.open('C:/Users/hp/Desktop/orginalmodel/orginalmodel/pic1.jpg')
image = image.resize((300, 200))
photo = ImageTk.PhotoImage(image)
self.canvas.create_image(0, 0, anchor='nw', image=photo)
self.canvas.image = photo
self.username_frame = tk.Frame(self.master)
self.username_frame.pack()
self.username_label = tk.Label(self.username_frame, text='用户名 ')
self.username_label.pack(side=tk.LEFT)
self.username_entry = tk.Entry(self.username_frame)
self.username_entry.pack(side=tk.LEFT)
self.password_frame = tk.Frame(self.master)
self.password_frame.pack()
self.password_label = tk.Label(self.password_frame, text=' 密码 ')
self.password_label.pack(side=tk.LEFT)
self.password_entry = tk.Entry(self.password_frame, show='*')
self.password_entry.pack(side=tk.LEFT)
self.login_button = tk.Button(self.master, text='登录', command=self.login)
self.login_button.pack()
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
if username == 'AAA' and password == '111':
self.master.destroy()
root = tk.Tk()
app = MainFrame(root)
app.mainloop()
else:
tk.messagebox.showerror('错误', '用户名或密码错误!')
```
这样就能够将`pic1.jpg`设置为登录界面的背景图片了。
阅读全文