我覺得這是你找什麼:
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