Reputation: 61
As the title suggests, I'm trying to create little search tool with GUI. How I want it to work: When I click the button 'search', open 'https://www.google.com/?gws_rd=cr&ei=qr5cU8GJGMnStAao1YG4BA#q=' + key words that I want to search for. These key words I will type in.
Whole code:
from tkinter import *
import webbrowser
class MainClass():
def __init__(self,master):
self.parent=master
self.gui()
def gui(self):
self.Source=StringVar()
#This next line I just tried out to use 'what' instead of 'str(self.Source) in def search(self)
what=Entry(myGUI, textvariable=self.Source).grid(row=9, column=2)
label4=Label(myGUI, text='Key words:', fg= 'Black').grid(row=9, column=1)
button4=Button(myGUI, text=" Search ", command=self.search).grid(row=18, column=1)
def search(self):
webbrowser.open('http://google.com/?gws_rd=cr&ei=qr5cU8GJGMnStAao1YG4BA#q=' + str(self.Source.get))
if __name__ == '__main__':
myGUI=Tk()
app=MainClass(myGUI)
myGUI.geometry("300x100+100+200")
myGUI.title('Google search')
myGUI.mainloop()
Well, the problem I am having is with this line:
def search(self):
webbrowser.open('http://google.com/?gws_rd=cr&ei=qr5cU8GJGMnStAao1YG4BA#q=' + str(self.Source.get))
If I leave it as it is and click the search button, it opens google and searches for: 'bound method StringVar.get of tkinter.StringVar object at 0x0301EE90'
If I keep the line, but instead of str(self.Source.get) I use str(self.Source), it again opens the google, but this time it searches for: PY_VAR0
If I use just self.Source, it gives me an error "Can't convert 'StringVar' object to str implicitly" when I press the search button.
So I am a little confused how to correctly use this, please help.
Upvotes: 2
Views: 1995
Reputation: 4604
You have to actually call the get method self.Source.get()
, otherwise, what you are providing to str
is the method, not its return value.
Thus, the whole line would be
webbrowser.open('http://google.com/?gws_rd=cr&ei=qr5cU8GJGMnStAao1YG4BA#q=' + str(self.Source.get()))
Upvotes: 2