2016-01-10 35 views
-2

試圖讓用戶輸入並進行搜索谷歌Tkinter的用戶輸入

例子:

客戶端的輸入條目=商業電子郵件
輸出=打開了 鉻的新選項卡>搜索谷歌的, 「INTEXT:gmail.com的商務郵件」

from tkinter import * 
import webbrowser 
root = Tk() 
def main(): 
     global userInput 
     root = Tk() 

Label2 = Label(text="GhostTest") 
Label2.pack() 

userInput = Entry(bd=2) 
userInput.pack() 

Button1 = Button(text="Search,", command=userInput) 
Button1.pack() 


def GoogleSearch(): 
     new = 2 
     userInput = Entry(bd=2) 
     term = Entry(userInput) 
     tabUrl = "http://google.com/?#q=" 
     webbrowser.open(tabUrl+str(term.get()),new=new); 

root.mainloop() 

if __name__=='__main__': 
    main() 
+1

問題是什麼?你收到錯誤信息還是什麼?如果你得到錯誤,然後把完整的錯誤信息。 – furas

+0

用正確的縮進更新,我遇到的問題是,當我按下搜索按鈕 – Ghost

+0

首先你應該說這是有問題的,它只是沒有做任何事情。 – furas

回答

1

你必須指定函數名(無括號)至command=

import tkinter as tk 
import webbrowser 

# --- functions --- 

def google_search(): 
    new = 2 

    # get user input 
    term = user_input.get() 

    tabUrl = "http://google.com/?#q=" 
    webbrowser.open(tabUrl+term, new=new); 

# --- main --- 

root = tk.Tk() 

lbl = tk.Label(text="GhostTest") 
lbl.pack() 

user_input = tk.Entry(bd=2) 
user_input.pack() 

# run `google_search` on button click 
btn = tk.Button(text="Search", command=google_search) 
btn.pack() 

root.mainloop() 
+0

工作過,謝謝,我會閱讀並瞭解它是如何工作的。 – Ghost

+0

http://effbot.org/tkinterbook/button.htm – furas