我觉得这是你找什么:
import Tkinter as tk
class Application(object):
def __init__(self):
self.root = tk.Tk()
# Create main window
self.root.title("Simple Password Program")
# Set title on main window
self.password = "Economics"
# The password
frame = tk.Frame(self.root)
# Frame to hold other widgets
frame.pack(expand=True, padx=10, pady=10)
label = tk.Label(frame, text="Enter Password: ")
# Label to display text before entry box
label.grid(row=0, column=0)
self.entry = tk.Entry(frame, width=50)
# Password entry box
self.entry.grid(row=0, column=1)
button = tk.Button(frame, text="Go", command=self.get_input, width=15)
# Button that calls get_input function when pressed
button.grid(row=1, column=0, columnspan=2)
self.root.bind_all("<Return>", self.get_input)
# Binds the Enter/Return key on your keyboard to the get_input function as an alternative to clicking the button
def get_input(self):
password = self.entry.get()
if password == self.password:
toplevel = tk.Toplevel(self.root)
# Opens another window
toplevel.resizable(width=False, height=False)
# Makes the second window not resizable by the user
frame = tk.Frame(toplevel)
# Frame to hold other widgets
frame.pack(padx=10, pady=10)
label = tk.Label(frame, text="Correct!")
# Another label to display text
label.pack()
button = tk.Button(frame, text="End Program", command=self.root.destroy)
# Button to close window
button.pack()
else:
toplevel = tk.Toplevel(self.root)
# Opens another window
toplevel.resizable(width=False, height=False)
# Makes the second window not resizable by the user
frame = tk.Frame(toplevel)
# Frame to hold other widgets
frame.pack(padx=10, pady=10)
label = tk.Label(frame, text="INCORRECT PASSWORD!")
# Another label to display text
label.pack()
button = tk.Button(frame, text="Dismiss", command=toplevel.destroy)
# Button to close window
button.pack()
app = Application()
# Create an object from the application class
tk.mainloop()
# Start tkinters mainloop
此代码是不完美的。这只是几分钟内我掀起的一个例子。只是看着这个,我怀疑你会学到很多东西,但无论如何我都很无聊。如果您想正确学习,请在Python模块上查找名为“Tkinter”的教程。它在您下载Python时预装,对于制作GUI非常有用。 Python中另一个值得注意的图形用户界面模块是“PyQt”,就像mkmostafa提到的那样。
P.S.我知道我的代码会跳过您在代码中执行的一些操作,例如倒计时。
你可能想看看[tkinter'模块](https://docs.python.org/3.5/library/tkinter.html)。另外,[这个资源](http://www.tkdocs.com/tutorial/index.html)可能更具有介绍性。 – icktoofay
我害怕为控制台写一个脚本并修改它作为一个GUI应用程序运行并不像使用'script'并用'GUI(脚本)'封装它那么简单。你的程序需要一个完全不同的体系结构。您将使用OOP(面向对象编程)来定义GUI元素,事件驱动编程以采取和响应用户输入,库以抽象的方式实现以跨平台方式显示像素的实际毛茸茸的实现......区别这两个并不是微不足道的。 – chucksmash