2015-09-07 104 views
-1

我是一个完整的初学者,并且希望有人能够将我在下面提供的改动过的代码放到一个简单的gui中。我不知道如何去做,并从看到的结果中学习。所以一旦我看到代码,我很可能会明白它是如何工作的以及我做错了什么。如何将我的基本python代码添加到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); 
+0

你可能想看看[tkinter'模块](https://docs.python.org/3.5/library/tkinter.html)。另外,[这个资源](http://www.tkdocs.com/tutorial/index.html)可能更具有介绍性。 – icktoofay

+1

我害怕为控制台写一个脚本并修改它作为一个GUI应用程序运行并不像使用'script'并用'GUI(脚本)'封装它那么简单。你的程序需要一个完全不同的体系结构。您将使用OOP(面向对象编程)来定义GUI元素,事件驱动编程以采取和响应用户输入,库以抽象的方式实现以跨平台方式显示像素的实际毛茸茸的实现......区别这两个并不是微不足道的。 – chucksmash

回答

0

如果你想使一个Python代码的GUI,我建议使用库“的PyQt”这对GUI实现了C++/Python的一个非常强大和简单的库。您还需要下载“Qt Designer”。这是一个IDE(类似于eclipse,如果你愿意)使用PyQt开发GUI。所以总结一下你需要:

  1. Python。
  2. PyQt库。
  3. Qt设计器。

如果您使用的是Windows,你可以从这里下载最后2项作为一个包:Qt Designer + PyQt

从二进制包窗口选择,根据你的Python优化版本Py2.7/3和基于你的windows 64和32位。

之后,按照本教程中关于如何使用Python和PyQt制作GUI HelloWorld应用程序。

Python GUI tutorial using PyQt

-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模块上查找名为“Tkinter”的教程。它在您下载Python时预装,对于制作GUI非常有用。 Python中另一个值得注意的图形用户界面模块是“PyQt”,就像mkmostafa提到的那样。

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

相关问题