By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

Need help with the get() method of a Text entry

P: n/a
I have a simple little program that brings up asks the user to enter a
note, then is supposed to place that note into a text file when the
user hits the submit button. However, when the user hits the submit
button, absolutely nothing happens. IDLE doesn't give an error
message and the note is not entered into the text file. For
troubleshooting puposes, I wanted to see if IDLE would at least print
the user's input; it doesn't do that either. Can someone please help
me?

Here is the code:

from Tkinter import *

class Application(Frame):
""" GUI application that creates a story based on user input. """
def __init__(self, master):
""" Initialize Frame. """
Frame.__init__(self, master)
self.grid()
self.create_widgets()

def create_widgets(self):
""" Create widgets to get note information. """
# create instruction label and text entry for notes
Label(self,
text = "Notes"
).grid(row = 0, column = 0, columnspan = 2 )
self.notes_ent = Text(self, width = 75, height = 10, wrap =
WORD)
self.notes_ent.grid(row = 2, column = 0 ,columnspan = 7,
rowspan = 3, sticky = W)
create submit button
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)
self.notes_ent.config(state=NORMAL)
Button(self,
text = "Add Note",
command = self.add_note(text1)
).grid(row = 1, column = 0, sticky = W)


def add_note(self, text1):
print text1
text_file = open("write_it.txt", "a+")
text_file.write(text1)


root = Tk()
root.title("Mad Lib")
app = Application(root)
root.mainloop()

Apr 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Chad wrote:
I have a simple little program that brings up asks the user to enter a
note, then is supposed to place that note into a text file when the
user hits the submit button. However, when the user hits the submit
button, absolutely nothing happens. IDLE doesn't give an error
message and the note is not entered into the text file. For
troubleshooting puposes, I wanted to see if IDLE would at least print
the user's input; it doesn't do that either. Can someone please help
me?

Here is the code:

from Tkinter import *

class Application(Frame):
""" GUI application that creates a story based on user input. """
def __init__(self, master):
""" Initialize Frame. """
Frame.__init__(self, master)
self.grid()
self.create_widgets()

def create_widgets(self):
""" Create widgets to get note information. """
# create instruction label and text entry for notes
Label(self,
text = "Notes"
).grid(row = 0, column = 0, columnspan = 2 )
self.notes_ent = Text(self, width = 75, height = 10, wrap =
WORD)
self.notes_ent.grid(row = 2, column = 0 ,columnspan = 7,
rowspan = 3, sticky = W)
create submit button
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)
self.notes_ent.config(state=NORMAL)
Button(self,
text = "Add Note",
command = self.add_note(text1)
).grid(row = 1, column = 0, sticky = W)

def add_note(self, text1):
print text1
text_file = open("write_it.txt", "a+")
text_file.write(text1)

root = Tk()
root.title("Mad Lib")
app = Application(root)
root.mainloop()
Your main problem is that you are calling add_note when you bind it. You
don't want to do that. It will get called when the event (button press)
happens. Most (or hopefully) all of the remedy is:

1. rename text1 to self.text1
2. change "command = self.add_note(text1)" to
"command = self.add_note"
3. change "def add_note(self, text1):" to
"def add_note(self):"
4. delete "print text1"
5. chane "text_file.write(text1)" to
"text_file.write(text1.get())"

I think I got everything.

James
Apr 12 '07 #2

P: n/a
Chad wrote:
I have a simple little program that brings up asks the user to enter a
note, then is supposed to place that note into a text file when the
user hits the submit button. However, when the user hits the submit
button, absolutely nothing happens. IDLE doesn't give an error
message and the note is not entered into the text file. For
troubleshooting puposes, I wanted to see if IDLE would at least print
the user's input; it doesn't do that either. Can someone please help
me?

Here is the code:

from Tkinter import *

class Application(Frame):
""" GUI application that creates a story based on user input. """
def __init__(self, master):
""" Initialize Frame. """
Frame.__init__(self, master)
self.grid()
self.create_widgets()

def create_widgets(self):
""" Create widgets to get note information. """
# create instruction label and text entry for notes
Label(self,
text = "Notes"
).grid(row = 0, column = 0, columnspan = 2 )
self.notes_ent = Text(self, width = 75, height = 10, wrap =
WORD)
self.notes_ent.grid(row = 2, column = 0 ,columnspan = 7,
rowspan = 3, sticky = W)
create submit button
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)
self.notes_ent.config(state=NORMAL)
Button(self,
text = "Add Note",
command = self.add_note(text1)
).grid(row = 1, column = 0, sticky = W)


def add_note(self, text1):
print text1
text_file = open("write_it.txt", "a+")
text_file.write(text1)


root = Tk()
root.title("Mad Lib")
app = Application(root)
root.mainloop()
On second look, it seems you have deeper problems, some of which are:
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)
You obviously added the second because the first will never work. Get
rid of both lines. Even if you intended the second, it immediately
nullifies the first. Instead of my previous suggestions, do this:

1. delete both "text1 =" lines
2. change "command = self.add_note(text1)" to
"command = self.add_note"
3. change "def add_note(self, text1):" to
"def add_note(self):"
4. delete "print text1"
5. change "text_file.write(text1)" to
"text_file.notes_ent.get(1.0, END)

James

Apr 12 '07 #3

P: n/a
James Stroud wrote:
Chad wrote:
>I have a simple little program that brings up asks the user to enter a
note, then is supposed to place that note into a text file when the
user hits the submit button. However, when the user hits the submit
button, absolutely nothing happens. IDLE doesn't give an error
message and the note is not entered into the text file. For
troubleshooting puposes, I wanted to see if IDLE would at least print
the user's input; it doesn't do that either. Can someone please help
me?

Here is the code:

from Tkinter import *

class Application(Frame):
""" GUI application that creates a story based on user input. """
def __init__(self, master):
""" Initialize Frame. """
Frame.__init__(self, master)
self.grid()
self.create_widgets()

def create_widgets(self):
""" Create widgets to get note information. """
# create instruction label and text entry for notes
Label(self,
text = "Notes"
).grid(row = 0, column = 0, columnspan = 2 )
self.notes_ent = Text(self, width = 75, height = 10, wrap =
WORD)
self.notes_ent.grid(row = 2, column = 0 ,columnspan = 7,
rowspan = 3, sticky = W)
create submit button
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)
self.notes_ent.config(state=NORMAL)
Button(self,
text = "Add Note",
command = self.add_note(text1)
).grid(row = 1, column = 0, sticky = W)


def add_note(self, text1):
print text1
text_file = open("write_it.txt", "a+")
text_file.write(text1)


root = Tk()
root.title("Mad Lib")
app = Application(root)
root.mainloop()

On second look, it seems you have deeper problems, some of which are:
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)

You obviously added the second because the first will never work. Get
rid of both lines. Even if you intended the second, it immediately
nullifies the first. Instead of my previous suggestions, do this:

1. delete both "text1 =" lines
2. change "command = self.add_note(text1)" to
"command = self.add_note"
3. change "def add_note(self, text1):" to
"def add_note(self):"
4. delete "print text1"
5. change "text_file.write(text1)" to
"text_file.notes_ent.get(1.0, END)

James
Or rather
5. change "text_file.write(text1)" to
"text_file.write(self.notes_ent.get(1.0, END))"
Apr 12 '07 #4

P: n/a
On Apr 12, 5:03 pm, James Stroud <jstr...@mbi.ucla.eduwrote:
James Stroud wrote:
Chad wrote:
I have a simple little program that brings up asks the user to enter a
note, then is supposed to place that note into a text file when the
user hits the submit button. However, when the user hits the submit
button, absolutely nothing happens. IDLE doesn't give an error
message and the note is not entered into the text file. For
troubleshooting puposes, I wanted to see if IDLE would at least print
the user's input; it doesn't do that either. Can someone please help
me?
Here is the code:
from Tkinter import *
class Application(Frame):
""" GUI application that creates a story based on user input. """
def __init__(self, master):
""" Initialize Frame. """
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create widgets to get note information. """
# create instruction label and text entry for notes
Label(self,
text = "Notes"
).grid(row = 0, column = 0, columnspan = 2 )
self.notes_ent = Text(self, width = 75, height = 10, wrap =
WORD)
self.notes_ent.grid(row = 2, column = 0 ,columnspan = 7,
rowspan = 3, sticky = W)
create submit button
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)
self.notes_ent.config(state=NORMAL)
Button(self,
text = "Add Note",
command = self.add_note(text1)
).grid(row = 1, column = 0, sticky = W)
def add_note(self, text1):
print text1
text_file = open("write_it.txt", "a+")
text_file.write(text1)
root = Tk()
root.title("Mad Lib")
app = Application(root)
root.mainloop()
On second look, it seems you have deeper problems, some of which are:
text1 = StringVar(self)
text1 = self.notes_ent.get(1.0, END)
You obviously added the second because the first will never work. Get
rid of both lines. Even if you intended the second, it immediately
nullifies the first. Instead of my previous suggestions, do this:
1. delete both "text1 =" lines
2. change "command = self.add_note(text1)" to
"command = self.add_note"
3. change "def add_note(self, text1):" to
"def add_note(self):"
4. delete "print text1"
5. change "text_file.write(text1)" to
"text_file.notes_ent.get(1.0, END)
James

Or rather
5. change "text_file.write(text1)" to
"text_file.write(self.notes_ent.get(1.0, END))"

Thank you, that worked very well.

Apr 12 '07 #5

P: n/a
Chad wrote:
On Apr 12, 5:03 pm, James Stroud <jstr...@mbi.ucla.eduwrote:
>James Stroud wrote:
>>Chad wrote:
I have a simple little program that brings up asks the user to enter a
note, then is supposed to place that note into a text file when the
user hits the submit button. However, when the user hits the submit
button, absolutely nothing happens. IDLE doesn't give an error
message and the note is not entered into the text file. For
troubleshooting puposes, I wanted to see if IDLE would at least print
the user's input; it doesn't do that either. Can someone please help
me?
[...]
[James second-guesses himself twice]
>
Thank you, that worked very well.
I thought it was particularly helpful of James to do the debugging for
you without waiting for you to point out the errors in his first and
second submissions. You just don't *get* help like that most places
nowadays.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
Apr 13 '07 #6

P: n/a
Chad wrote:
On Apr 12, 5:03 pm, James Stroud <jstr...@mbi.ucla.eduwrote:
>James Stroud wrote:
>>Chad wrote:
I have a simple little program that brings up asks the user to enter a
note, then is supposed to place that note into a text file when the
user hits the submit button. However, when the user hits the submit
button, absolutely nothing happens. IDLE doesn't give an error
message and the note is not entered into the text file. For
troubleshooting puposes, I wanted to see if IDLE would at least print
the user's input; it doesn't do that either. Can someone please help
me?
[...]
[James second-guesses himself twice]
>
Thank you, that worked very well.
I thought it was particularly helpful of James to do the debugging for
you without waiting for you to point out the errors in his first and
second submissions. You just don't *get* help like that most places
nowadays.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 13 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.