Reputation: 297
(Python 3.3) With no formal instruction, I am trying to advance my very basic knowledge of Python into Tkinter. Below is a program I am trying to write. I have two problems:
1 - When I run this, both radio buttons are active until I click one. Not a huge deal, but it annoys me.
2 - I can't seem to get the .get() method to pull the data entered in the Entry box from the data_window function to the check_data function. As my program eludes, the next step after it passes the check data function will be to send it on to the formatEntry function, which is not written yet. But until I figure this put, that obviously won't work either.
I have a functional version of this without using TKinter and I am trying to adapt that knowledge into this for the purpose of learning. So please feel free to point out any other problems you see, just please keep the explanations very basic for a newb! Thanks
from tkinter import *
import os, shelve
food_entry = None
# Opens a new window for entering food items
def data_window():
global food_entry
new_window_entry = Tk()
new_window_entry.title('Data Entry Window')
Label(new_window_entry, text = 'Enter the food items you have eaten below, separated by commas:').grid(sticky = W, columnspan = 2)
Label(new_window_entry, text = '(i.e. Apple, Eggs, Ground Beef)').grid(sticky = W, columnspan = 2)
food_entry = Text(new_window_entry, width = 55, height = 2, wrap = WORD)
food_entry.grid(sticky = W, columnspan = 2)
#food_entry = Entry(new_window_entry)
Label(new_window_entry, text = 'Within two hours after eating, did you experience any of the following symptoms:').grid(sticky = W, columnspan = 2)
Label(new_window_entry, justify = LEFT, wraplength = 450, text = 'bloating, diarrhea, nausea, vomiting, irritable bowel, skin rashes, fatigue, joint pain, dark circles under the eyes, night sweats, or tingling or swelling of the face, fingers, feet or other extemities?').grid(sticky = W, columnspan = 2)
inflam = StringVar(master = new_window_entry)
Radiobutton(new_window_entry, text = 'Yes', variable = inflam, value = 'y').grid(row = 9, column = 0, sticky = E)
Radiobutton(new_window_entry, text = 'No', variable = inflam, value = 'n').grid(row = 9, column = 1, sticky = W)
Button(new_window_entry, text = 'Submit', command = check_data).grid(row = 10, column = 0, sticky = W + E)
Button(new_window_entry, text = 'Cancel', command = new_window_entry.destroy).grid(row = 10, column = 1, sticky = W + E)
#Check to ensure all fields have been entered
#Convert entry into formatted list
new_window_entry.mainloop()
def check_data():
global food_entry
print(food_entry.get(1.0, END))
if food_entry.get(1.0, END) == None:
print('Nothing Entered')
# tkMessageBox.showwarning(message = 'Please complete all fields of the form.')
else:
print('Next Function')
# formatEntry()
root = Tk()
root.title('')
Label(root, text = 'Food Tracker v3.0').grid(columnspan = 2)
Button(root, text = 'Enter Data', command = data_window).grid(row = 1, column = 0, sticky = W)
Button(root, text = 'View Data', command = view_window).grid(row = 1, column = 1, sticky = E)
Upvotes: 0
Views: 2279
Reputation: 5289
First of all you defined food_entry
twice, and the second time you never did anything with it (food_entry = Entry(new_window_entry)
). You'll need to remove that 2nd definition.
Next, this assigns what grid()
returns to food_entry
, which is None
food_entry = Text(new_window_entry, width = 55, height = 2, wrap = WORD).grid(sticky = W, columnspan = 2)
So instead you'll need to grid()
on a separate line. This keeps the Text()
object assigned to food_entry
as you intended:
food_entry = Text(new_window_entry, width = 55, height = 2, wrap = WORD)
food_entry.grid(sticky = W, columnspan = 2)
Finally, when accessing a Text()
with get()
you need to specifiy the start and ending points that you're trying to obtain:
...
print(food_entry.get(1.0, END))
if food_entry.get(1.0, END) == None:
....
Edit: One last thing to cover your question about the Radiobutton()
displaying as selected initially, you can correct this by explicitly declaring the master
of your StringVar()
:
inflam = StringVar(master = new_window_entry)
Edit 2: For your if / else statement not working:
if food_entry.get(1.0, END) == None:
This is checking to see if the string in the Text is None, which it's not, it's actually an empty string, so we need to check it accordingly:
if food_entry.get(1.0, END).strip() == '': # Use .strip() to remove spaces and newlines
Here's a complete code sample:
from tkinter import *
import os, shelve
food_entry = None
# Opens a new window for entering food items
def data_window():
global food_entry
new_window_entry = Toplevel() # Change to Toplevel (a popup) instead of a new Tk instance
new_window_entry.title('Data Entry Window')
Label(new_window_entry, text = 'Enter the food items you have eaten below, separated by commas:').grid(sticky = W, columnspan = 2)
Label(new_window_entry, text = '(i.e. Apple, Eggs, Ground Beef)').grid(sticky = W, columnspan = 2)
food_entry = Text(new_window_entry, width = 55, height = 2, wrap = WORD)
food_entry.grid(sticky = W, columnspan = 2)
Label(new_window_entry, text = 'Within two hours after eating, did you experience any of the following symptoms:').grid(sticky = W, columnspan = 2)
Label(new_window_entry, justify = LEFT, wraplength = 450, text = 'bloating, diarrhea, nausea, vomiting, irritable bowel, skin rashes, fatigue, joint pain, dark circles under the eyes, night sweats, or tingling or swelling of the face, fingers, feet or other extemities?').grid(sticky = W, columnspan = 2)
Radiobutton(new_window_entry, text = 'Yes', variable = inflam, value = 'y').grid(row = 9, column = 0, sticky = E)
Radiobutton(new_window_entry, text = 'No', variable = inflam, value = 'n').grid(row = 9, column = 1, sticky = W)
Button(new_window_entry, text = 'Submit', command = check_data).grid(row = 10, column = 0, sticky = W + E)
Button(new_window_entry, text = 'Cancel', command = new_window_entry.destroy).grid(row = 10, column = 1, sticky = W + E)
#new_window_entry.mainloop() No need for this, the mainloop is already running
def check_data():
global food_entry
print(food_entry.get(1.0, END))
if food_entry.get(1.0, END).strip() == '':
print('Nothing Entered')
else:
print('Next Function')
root = Tk()
inflam = StringVar() # move inflam init to a broader scope so that the buttons don't but
inflam.set('n') # initialize it as 'n'
root.title('')
Label(root, text = 'Food Tracker v3.0').grid(columnspan = 2)
Button(root, text = 'Enter Data', command = data_window).grid(row = 1, column = 0, sticky = W)
#Button(root, text = 'View Data', command = view_window).grid(row = 1, column = 1, sticky = E)
root.mainloop()
Upvotes: 1