2015-09-07 43 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.我知道我的代碼會跳過您在代碼中執行的一些操作,例如倒計時。

相關問題