[英]How can I add my basic python code into a gui
我是一個完整的初學者,希望有人可以將下面提供的修改后的代碼段放入一個簡單的 gui 中。 我不知道該怎么做,並從看到結果中學習得很好。 所以一旦我看到代碼,我很可能會理解它是如何工作的以及我做錯了什么。
import time
print("Please enter the password.")
Pass = input("Password: ")
if Pass==("Economics"):
print("Password Correct, now entering program!");
print("Gathering Information..."); time.sleep(0.5);
print("Fetching answers...");
else:
print("Incorrect login credentials, self destructing in 5")
time.sleep(1.0);
print("4");
time.sleep(1.0);
print("3");
time.sleep(1.0)
print("2");
time.sleep(1.0);
print("1")
time.sleep(1.0);
print("Joking! Try to login again.")
print("Please enter the password.")
Pass = input("Password: ")
if Pass==("Economics"):
print("Your password is correct, you can now progress to the quiz");
print("Gathering Information..."); time.sleep(0.5);
如果您想為 Python 代碼制作 GUI,我建議使用庫“PyQt”,它是一個非常強大且簡單的庫,用於 C++/Python 的 GUI 實現。 您還需要下載“Qt Designer”。 它是一個使用 PyQt 開發 GUI 的 IDE(如果你願意的話,類似於 eclipse)。 總而言之,您需要:
如果您使用的是 Windows,您可以從這里下載最后 2 個項目作為一個包: Qt Designer + PyQt
根據您的 python 版本 Py2.7/3 和您的 Windows 版本 64 與 32 位,從 Windows 二進制包中進行選擇。
之后,請遵循本教程,了解如何使用 Python 和 PyQt 制作 GUI HelloWorld 應用程序。
我認為這就是你要找的:
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
這段代碼並不完美。 這只是我在幾分鍾內提出的一個例子。 只是看着這個,我懷疑你會學到很多東西,但我很無聊,所以無論如何。 如果您想正確學習,請查閱名為“Tkinter”的 Python 模塊的教程。 它是在您下載 Python 時預裝的,非常適合制作 GUI。 Python 中另一個值得注意的 GUI 模塊是 mkmstafa 提到的“PyQt”。
PS 我知道我的代碼跳過了你在代碼中所做的一些事情,比如倒計時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.