我做了一个登录面板,但是我不知道如何将代码附加到文件中, 例如,如果我运行一个.exe文件,它将首先运行我的代码,并且如果正确填写了这些条目,则该.exe文件将运行。 我的代码:
from tkinter import *
import tkinter.messagebox
import os`
username = 'Zsolti'
password = 'zsoltika2005'
def WindowVariables():
global UsernameE
global PasswordE
def CheckLogin():
if UsernameE.get() == username and PasswordE.get() == password:
tkinter.messagebox.showinfo('Login', 'Successfully logged in as:
Zsolti')
else:
tkinter.messagebox.showinfo('Login', 'Login failed')
window = Tk()
MainFrame = Frame(width=110, height=40)
MainFrame.grid()
window.title('Login Panel')
UsernameL = Label(window, text='Enter username')
UsernameL.grid(row=0, column=0, sticky='e')
PasswordL = Label(window, text='Enter password')
PasswordL.grid(row=1, column=0, sticky='e')
UsernameE = Entry(window, textvariable=username)
UsernameE.grid(row=0, column=1, sticky='w')
PasswordE = Entry(window, show='*', textvariable=password)
PasswordE.grid(row=1, column=1, sticky='w')
LoginButton = Button(window, text='Login!', command=CheckLogin)
LoginButton.grid(row=2, columnspan=2)
window.mainloop()
答案 0 :(得分:1)
这应该为您工作,当它正确登录时,它将调用另一个exe文件,然后希望终止登录窗口,
from tkinter import *
import tkinter.messagebox
import subprocess
import sys
username = 'Zsolti'
password = 'zsoltika2005'
def WindowVariables():
global UsernameE
global PasswordE
def CheckLogin():
if UsernameE.get() == username and PasswordE.get() == password:
tkinter.messagebox.showinfo('Login', 'Successfully logged in as: Zsolti')
subprocess.Popen([r"Important File Path.exe"])
sys.exit()
else:
tkinter.messagebox.showinfo('Login', 'Login failed')
window = Tk()
MainFrame = Frame(width=110, height=40)
MainFrame.grid()
window.title('Login Panel')
UsernameL = Label(window, text='Enter username')
UsernameL.grid(row=0, column=0, sticky='e')
PasswordL = Label(window, text='Enter password')
PasswordL.grid(row=1, column=0, sticky='e')
UsernameE = Entry(window, textvariable=username)
UsernameE.grid(row=0, column=1, sticky='w')
PasswordE = Entry(window, show='*', textvariable=password)
PasswordE.grid(row=1, column=1, sticky='w')
LoginButton = Button(window, text='Login!', command=CheckLogin)
LoginButton.grid(row=2, columnspan=2)
window.mainloop()