Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown syntax error on creating a simple widget in Tkinter

I was following this tutorial (http://sebsauvage.net/python/gui/#add_button) on making widgets with Tkinter. I have been making sure to follow it very carefully but, when I run it now in step 10, I get an "Invalid Syntax" Error. Here the code:

import tkinter

class simpleapp_tk(tkinter.Tk):
    def __init__(self,parent):
        tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entry = tkinter.Entry(self)
        self.entry.grid(column=0,row=0,sticky='EW')

        button = tkinter.Button(self,text=u"Click me !")
        button.grid(column=1,row=0)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

The IDLE points the error is in this line, selecting the second quotation marks:

button = tkinter.Button(self,text=u"Click me !**"**)

The tutorial was written in Python 2, but I'm using Python 3. Can anyone see what is the error and what to do to fix it (in Python 3)?

Thanks in advance for any help, I am new to programming and English is not my native language.

like image 778
craftApprentice Avatar asked Feb 14 '26 19:02

craftApprentice


2 Answers

Replace u"Click me !**" with "Click me !**"

The u indicates a Unicode string (type unicode instead of str) in Python 2, but in Python 3, the distinction between the str and unicode types is gone and the u is scrapped.

like image 64
Fred Foo Avatar answered Feb 16 '26 09:02

Fred Foo


There is no u prefix for unicode strings in Python 3.

like image 44
patrys Avatar answered Feb 16 '26 08:02

patrys