Reputation: 25
I'm trying to display text from a database on the screen once a button is pressed. Here is my code:
root = Tk()
root.geometry("800x500")
vfr_import = PhotoImage(file="Images/vfr.png")
r_vfr = vfr_import.subsample(3, 3)
class Airfields(Button):
def __init__(self, master, image, command, location):
Button.__init__(self, master, image=image, command=command)
self.style = {"bg":"#7D7D7D","bd":0,"highlightbackground":"#7D7D7D","highlightthickness":0}
self.place(x=location[0], y=location[1])
self.config(self.style)
class TextBox(Text):
def __init__(self, master, text, location):
Text.__init__(self, master, text=text)
self.style = {"bg":"Black","font":"(Arial, 12)"}
self.place(x=location[0], y=loaction[1])
self.config(self.style)
def display_info(location):
name = TextBox(root, str(c.execute("""SELECT Name FROM Airfields WHERE ICAO = (?)""", (location,))), [500,300])
Andrewsfield = Airfields(root, r_vfr, display_info('EGSL'), [255, 375])
However I get the error TclError: unknown option "-text"
Upvotes: 2
Views: 296
Reputation: 141
As Bryan Oakley said, the Text
widget doesn't use a -text
parameter to insert text. The Text
widget has a method called insert
, which takes an index for the first parameter and chars in the second. Tags and additional characters can follow.
def insert(self, index, chars, *args): Insert CHARS before the characters at INDEX. An additional tag can be given in ARGS. Additional CHARS and tags can follow in ARGS.
So your code would look like this: (I've formatted it to PEP8 style guidelines)
BTW your code indent should be four spaces, not two.
root = Tk()
root.geometry("800x500")
vfr_import = PhotoImage(file="Images/vfr.png")
r_vfr = vfr_import.subsample(3, 3)
class Airfields(Button):
def __init__(self, master, image, command, location):
Button.__init__(self, master, image=image, command=command)
self.style = {"bg": "#7D7D7D", "bd": 0,
"highlightbackground": "#7D7D7D", "highlightthickness": 0}
self.place(x=location[0], y=location[1])
self.config(self.style)
class TextBox(Text):
def __init__(self, master, text, location):
Text.__init__(self, master)
self.insert(0, text)
self.style = {"bg": "Black", "font": "(Arial, 12)"}
self.place(x=location[0], y=loaction[1])
self.config(self.style)
def display_info(location):
name = TextBox(root, str(c.execute(
"""SELECT Name FROM Airfields WHERE ICAO = (?)""", (location,))), [500, 300])
Andrewsfield = Airfields(root, r_vfr, display_info('EGSL'), [255, 375])
Upvotes: 1