[英]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.