when I try to run cursor.execute("SELECT VERSION()")
inside my class method it gives me an SyntaxError. But works just fine outside the class, the same code. I'm brand new to Python, have tried search and so on but could not find anything.
The error I get is following:
cursor.execute("SELECT VERSION()")
^
SyntaxError: invalid syntax
and the code I try to run, looks like:
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
import tkMessageBox
import MySQLdb
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
Label(self, text="Username").grid(row=0)
Label(self, text="Password").grid(row=1)
Label(self, text="Database").grid(row=2)
self.username = Entry(self)
self.username.grid(row=0, column=1)
self.password = Entry(self)
self.password.grid(row=1, column=1)
self.database = Entry(self)
self.database.grid(row=2, column=1)
Button(self, text='Show', command=self.show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
def show_entry_fields(self):
try:
db = MySQLdb.connect("localhost", "root", "", "python" )
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
db.close()
except:
tkMessageBox.showinfo("Say Hello", "Dont work.")
root = Tk()
root.title("Simple GUI")
root.resizable(width = FALSE, height = FALSE)
root.geometry("700x500")
# Create the frame and add it to the grid
app = Application(root)
root.mainloop()
You've mixed tabs and spaces. The
cursor.execute
line has 4 spaces instead of one of the tabs it should have, causing the indentation to be misaligned. Turn on "show whitespace" in your editor to see things like this easier.