I'm trying to take the String from an Entry widget and store that to a variable. I'm using a menu item in tkinter to do this I have tried many different things and I still can't get it to work. This is my first python program.
from tkinter import *
class application(Frame):
""" A Gui application with three buttons"""
def __init__(self, master = None):
""" Inititalization the fram"""
Frame.__init__(self,master)
self.master = master
#self.grid()
self.create_widgets()
def create_widgets(self):
self.master.title("My GUI")
self.pack(fill=BOTH, expand=1)
buttons1 = Button(self,text = "this wroks", command= self.program_button)
buttons1.place(x=250,y=250)
#self.buttons1.place(x=5, y=10)
menu = Menu(self.master)
self.master.config(menu=menu)
test = StringVar()
eBox = Entry(self, textvariable=test)
#eBox.pack()
eBox.grid()
file = Menu(menu)
file.add_command(label='Save', command=self.text_capture)
file.add_command(label='Exit', command=self.program_exit)
menu.add_cascade(label='File', menu=file)
def program_button(self):
exit()
def program_exit(self):
exit()
def text_capture(test):
#test = StringVar()
mtext = test.get()
print(test) # this prints py_Var1 ???
print(mtext)
print(test.get())
root = Tk()
root.geometry("500x500")
app = application(root)
root.mainloop()
I have changed some of your code but on the whole you were pretty close to your wanted end code. The bit i have changed are the following:
test = StringVar()
eBox = Entry(self, textvariable=test)
to
self.test = StringVar()
eBox = Entry(self, textvariable=self.test)
and
def text_capture(test):
mtext = test.get()
print(test)
print(mtext)
print(test.get())
to
def text_capture(self):
mtext = self.test.get()
print(self.test)
print(mtext)
print(self.test.get())
The full working code is below:
from tkinter import *
class application(Frame):
""" A Gui application with three buttons"""
def __init__(self, master = None):
""" Inititalization the fram"""
Frame.__init__(self,master)
self.master = master
#self.grid()
self.create_widgets()
def create_widgets(self):
self.master.title("My GUI")
self.pack(fill=BOTH, expand=1)
buttons1 = Button(self,text = "this wroks", command= self.program_button)
buttons1.place(x=250,y=250)
#self.buttons1.place(x=5, y=10)
menu = Menu(self.master)
self.master.config(menu=menu)
self.test = StringVar()
eBox = Entry(self, textvariable=self.test)
#eBox.pack()
eBox.grid()
file = Menu(menu)
file.add_command(label='Save', command=self.text_capture)
file.add_command(label='Exit', command=self.program_exit)
menu.add_cascade(label='File', menu=file)
def program_button(self):
exit()
def program_exit(self):
exit()
def text_capture(self):
mtext = self.test.get()
print(self.test)
print(mtext)
print(self.test.get())
root = Tk()
root.geometry("500x500")
app = application(root)
root.mainloop()