如何将我的基本python代码添加到gui中

时间:2015-09-07 00:04:59

标签: python user-interface

我是一个完全的初学者,并希望如果有人可以将我提供的改变后的代码段放到一个简单的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);

2 个答案:

答案 0 :(得分:0)

如果您想为python代码制作GUI,我建议使用库" PyQt"它是一个非常强大而简单的C ++ / Python GUI实现库。你需要下载" Qt Designer"同样。它是一个IDE(类似于eclipse,如果你愿意)用于使用PyQt开发GUI。总结一下,你需要:

  1. 的Python。
  2. PyQt库。
  3. Qt Designer。
  4. 如果您使用的是Windows,则可以从此处下载最后2项作为包:Qt Designer + PyQt

    根据您的python版本Py2.7 / 3并基于您的Windows版本64对32位,从Windows的二进制包中选择。

    之后按照本教程学习如何使用Python和PyQt制作GUI HelloWorld应用程序。

    Python GUI tutorial using PyQt

答案 1 :(得分:-2)

我认为这就是你要找的东西:

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模块上查找名为&#34; Tkinter&#34;的教程。它是在你下载Python时预装的,非常适合制作GUI。 Python中另一个值得注意的GUI模块是&#34; PyQt&#34;正如mkmostafa所说。

P.S。我知道我的代码会跳过您在代码中执行的一些操作,例如倒计时。