Tkinter用户输入

时间:2016-01-10 16:02:52

标签: python tkinter tk

尝试获取客户输入并进行谷歌搜索

示例:

  

客户的输入条目=商业电子邮件
  output =打开一个新选项卡   铬>搜索谷歌,“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 个答案:

答案 0 :(得分: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()