0

Ive got this json response from firebase using the firebase python library. Everything works by im having a hard time decoding the json response to strings so i can view them in tkinter. Any help would be great thanks.

import requests
from firebase import firebase
from Tkinter import *

firebase = firebase.FirebaseApplication('https://-----', None) #hiding the url for security
message = firebase.get('/message', None)
name = firebase.get('/name', None)

print("The message is: ",message," and it's from ",name)


master = Tk()
showmessage = message," from ", name
w = Label(master, text=showmessage)
w.pack()

mainloop()

what im getting looks like this

#printing
('The message is: ', '"this is my message"', " and it's from ",'from a name')

#in tkinter
{"this is my message"}{"from a name"}
user2758113
  • 1,001
  • 1
  • 13
  • 25
  • You've shown what your program actually produces. Tell us what you'd like it produce. – Robᵩ Oct 19 '13 at 03:08
  • Please paste a **complete** program into your question. See http://SSCCE.ORG for more info. – Robᵩ Oct 19 '13 at 03:24
  • I'm confused why you think you have a JSON response from firebase. You appear to have strings, not JSON objects. – Robᵩ Oct 19 '13 at 03:28

2 Answers2

0

You want showmessage to take on the value of the string by itself? Right now you are making a tuple out of it with the commas, so you can either add up the strings (concatenate), or use formatting...

# just join the strings...
showmessage = message + " from " + name

# or string formatting
showmessage = "{0} from {1}".format(message,name)

In either case, showmessage should now be a string containing this is my message from from a name (in the way you show it in your question...)

beroe
  • 11,784
  • 5
  • 34
  • 79
  • So printing this works, but for the tkinter it is still showing the {brackets} – user2758113 Oct 19 '13 at 03:19
  • The brackets end up there if the string is not a string... See [this question](http://stackoverflow.com/a/8303033/1681480). Lots of discussion that the `.label` method works better than `Label()`... – beroe Oct 19 '13 at 03:29
  • Are you sure you removed ALL the other `showmessage` lines and the one you are sending is the same as the one you are printing? There should be no parentheses or anything around the printed version even if the content of the message looks right. – beroe Oct 19 '13 at 03:30
  • I am very sorry you are right, i forgot to delete another showmessage. – user2758113 Oct 19 '13 at 03:32
0

Try these lines instead:

print("The message is: {} and it's from {}".format(message,name))


showmessage = message+" from "+ name
w = Label(master, text=showmessage)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • So printing this works, but for the tkinter it is still showing the {brackets} – user2758113 Oct 19 '13 at 03:20
  • With respect, I don't believe you. Double-check that you have create `showmessage` correctly. Add `print (type(showmessage))` to confirm that it isn't a tuple. Add `print(showmessage)` also. – Robᵩ Oct 19 '13 at 03:26
  • The printing works but for some reason the tkinter is still showing {brackets} around the text. This is so weird because when I print it is perfect, but tkinter for some reason is showing brackets. – user2758113 Oct 19 '13 at 03:29
  • Tkinter prints the brackets if you pass in a sequence instead of a string. I believe you are NOT passing in a string. 1) What does `print(type(showmessage))` reveal? 2) What does [this program](http://ideone.com/gHPybx) do when you run it? – Robᵩ Oct 19 '13 at 03:32