2014-03-01 128 views
4

如何創建一個使用兩個按鈕創建窗口的函數,其中每個按鈕都具有指定的字符串,並且如果單擊它,將返回指定的變量?類似於這個視頻中的@ 3:05 https://www.khanacademy.org/science/computer-science-subject/computer-science/v/writing-a-simple-factorial-program---python-2(我知道這是一個非常簡單的初學者程序的教程,但它是我能找到的唯一視頻),但沒有文本框,並且我對「確定」和「 '取消'按鈕可以。如何使用python中的按鈕創建窗口

我是否必須創建一個窗口,在裏面用一個字符串繪製一個矩形,然後創建一個檢查鼠標移動/鼠標點擊的循環,然後在鼠標座標位於其中一個按鈕,並點擊鼠標? 或者是否有一個函數/一組函數可以使按鈕的窗口更容易?或者一個模塊?

回答

8

概述

不,你不必「畫一個矩形,然後做一個循環」。 必須做的是導入某種GUI工具箱,並使用該工具箱內置的方法和對象。一般來說,其中一種方法是運行一個循環來監聽事件並根據這些事件調用函數。這個循環稱爲事件循環。所以,雖然這樣的循環必須運行,但您不必創建循環。

注意事項

如果你正在尋找從這樣的提示打開的窗口。你鏈接到視頻,該問題是一個有點艱難。這些工具包的設計不是以這種方式使用。通常,您可以編寫完整的基於GUI的程序,其中所有輸入和輸出均通過小部件完成。這不是不可能的,但在我看來,學習時應該堅持所有文本或所有GUI,而不是混合兩者。使用的Tkinter

例如

實施例,一個這樣的工具包是Tkinter的。 Tkinter是python內置的工具包。任何其他工具包,如wxPython,PyQT等都將非常相似,並且工作得很好。 Tkinter的優勢在於您可能已經擁有了它,它是學習GUI編程的絕佳工具包。對於更高級的編程來說也是很棒的,儘管你會發現那些不同意這一點的人。不要聽他們。

以下是Tkinter中的一個示例。這個例子在python 2.x中有效。對於python 3.x,您需要從tkinter而不是Tkinter導入。

import Tkinter as tk 

class Example(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     # create a prompt, an input box, an output label, 
     # and a button to do the computation 
     self.prompt = tk.Label(self, text="Enter a number:", anchor="w") 
     self.entry = tk.Entry(self) 
     self.submit = tk.Button(self, text="Submit", command = self.calculate) 
     self.output = tk.Label(self, text="") 

     # lay the widgets out on the screen. 
     self.prompt.pack(side="top", fill="x") 
     self.entry.pack(side="top", fill="x", padx=20) 
     self.output.pack(side="top", fill="x", expand=True) 
     self.submit.pack(side="right") 

    def calculate(self): 
     # get the value from the input widget, convert 
     # it to an int, and do a calculation 
     try: 
      i = int(self.entry.get()) 
      result = "%s*2=%s" % (i, i*2) 
     except ValueError: 
      result = "Please enter digits only" 

     # set the output widget to have our result 
     self.output.configure(text=result) 

# if this is run as a program (versus being imported), 
# create a root window and an instance of our example, 
# then start the event loop 

if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 
+0

很好的例子。能否請您解釋一下什麼是'補=」 既 「','補=」 X 「','padx = 20'和'錨=」 W「'做。 –

+0

@AlexanderCska :你可以通過閱讀tkinter文檔得到這些問題的答案。一個好的開始是http://effbot.org/tkinterbook/pack.htm –

0

你應該看看wxpython,這是一個GUI庫,如果你有一些python的知識,這個庫很容易開始。

下面的代碼將創建一個窗口,供您(source):

import wx 

app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window. 
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window. 
frame.Show(True)  # Show the frame. 
app.MainLoop() 

看看這個section(如何創建按鈕)。但是從installation instructions開始。

0
#Creating a GUI for entering name 
def xyz(): 
    global a 
    print a.get() 
from Tkinter import * 
root=Tk() #It is just a holder 
Label(root,text="ENter your name").grid(row=0,column=0) #Creating label 
a=Entry(root)   #creating entry box 
a.grid(row=7,column=8) 
Button(root,text="OK",command=xyz).grid(row=1,column=1) 
root.mainloop()   #important for closing th root=Tk() 

這是最基本的一個。

-2

這裏是我的方法,用一個名爲「Hello!」的按鈕創建一個窗口。 當關閉時,會打開一個新窗口,顯示「Cool!「

from tkinter import * 
def hello(event): 
    print("Single Click, Button-l") 
def Cool(event):       
    print("That's cool!") 

widget = Button(None, text='Hello!') 
widget.pack() 
widget.bind('<Button-1>', Hello) 
widget.mainloop() 

widget = Button(None, text='Cool!') 
widget.pack() 
widget.bind('<Double-1>', Cool) 
+0

你爲什麼在調用'mainloop'後有代碼?那些代碼不會做你認爲它的事情,你也將一個方法'Hello'綁定到一個事件上,但是你的方法被命名爲'hello'。另外,最好的做法是你不應該在按鈕上使用'bind';它有一個'command用於將按鈕連接到某個功能的屬性。 –

相關問題