Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to fix 'uncaught exception of type NSException' in Python
#1
I'm currently making a database using MySql in Python in order to collect information about hospitals. I've created different programs (like one for the registration form, one for the database, and so on.)
and imported them (To execute them) in various parts of my other programs. However, one of them does not perform its function and raises an error.

Here is the overall procedure:

. Make a program to create a database using MySql
. Make a program to create 2 buttons using Pygame (Register and login)
. Make the program to create a registration form when Register is clicked.
. Send the details of it to the database

Overall, the program works if the Pygame buttons program isn't included. The Database program executes the Registration form program, which obtains the details entered, and is sent by the Database programme to the database created. However, executing the Registration Form when I click on the Register button (in the Buttons program) raises an error(uncaught exception of type NSException') and python closes. I've tried many ways of doing the same, even of copy-pasting the code into the program itself, but they both yield the same result.

Could anyone help to correct this error
Here is the code I've used for the Button:
import pygame
pygame.init()
class button():
    def __init__(self, colour, x, y, width, height, text=''):
        self.colour = colour
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self, win, outline=None):
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

        pygame.draw.rect(win, self.colour, (self.x, self.y, self.width, self.height), 0)

        if self.text != '':
            font = pygame.font.SysFont('comicsans', 35)
            text = font.render(self.text, 1, (0, 0, 0))
            win.blit(text, (
            self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))

    def isOver(self, pos):
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False


def redrawGameWindow():
    win.fill((255, 255, 255))
    loginButton.draw(win)
    registerButton.draw(win)


def register():
    import Databases



run = True
loginButton = button((0, 255, 0), 175, 150, 150, 60, 'Login')
registerButton = button((255, 0, 0), 175, 300, 150, 60, 'Register')
while run:
    redrawGameWindow()
    pygame.display.update()

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if loginButton.isOver(pos):
                print("Login")
            if registerButton.isOver(pos):
                register()

        if event.type == pygame.MOUSEMOTION:
            if loginButton.isOver(pos):
                loginButton.colour = (0, 175, 0)
            else:
                loginButton.colour = (0, 255, 0)
            if registerButton.isOver(pos):
                registerButton.colour = (255, 0, 0)
            else:
                registerButton.colour = (175, 0, 0)


Here is the code I've used for the Registration Form:


from tkinter import *
tk = Tk()
tk.geometry('500x500')
tk.title("Registration Form")

Heading = Label(tk, text = "Registration Form", width = 20, font = ('bold', 20))
Heading.place(x = 120, y = 53)

hospitalName = Label(tk, text = "Hospital Name", width = 20, font = ('comicsans', 15))
hospitalName.place(x = 80, y = 130 )

entryBox_hosp = Entry(tk)
entryBox_hosp.place(x = 229, y = 130)

password = Label(tk, text = "Password", width = 20, font = ('comicsans', 15))
password.place(x = 75, y = 180)

entryBox_pass = Entry(tk)
entryBox_pass.place(x = 229, y = 180)

coordinates = Label(tk, text = "Coordinates", width = 20, font = ('comicsans', 15))
coordinates.place(x = 80, y = 230)

entryBox_crds = Entry(tk)
entryBox_crds.place(x = 229, y = 230)

def transfer():
    global hosp_name
    global password
    global coordinates
    hosp_name = entryBox_hosp.get()
    password = entryBox_pass.get()
    coordinates = entryBox_crds.get()

submit = Button(tk, text = "Submit", fg = 'red', width = 20, command = transfer)
submit.place(x = 150, y = 350)

mainloop()
For the Database:
import mysql.connector
import Registration_Form
mydb = mysql.connector.connect(
    host = #,
    user = #,
    passwd = #,
    database = #
)

myCursor = mydb.cursor()

sqlFormula = "INSERT INTO UserInfo (hosp_name, password, coordinates) VALUES (%s, %s, %s)"

hospital = [Registration_Form.hosp_name, Registration_Form.password, Registration_Form.coordinates]

myCursor.execute(sqlFormula, hospital)
mydb.commit()
NOTE: The database has already been created. These are just the steps of populating my database (The one which is being executed)
scidam wrote Jul-09-2019, 07:05 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. Refer to BBCode help topic on how. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Type Error: bad operand type for unary +: 'str' Psypher1 2 111 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  Getting type from input() function in Python 3.0 leodavinci1990 7 243 Jul-29-2019, 08:28 PM
Last Post: avorane
  problem using custom exception handling in python srm 3 181 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  'Exception Has occured: UnBoundLocalError' caston 1 171 Jun-12-2019, 02:33 PM
Last Post: perfringo
  an easy way to disable exception handling Skaperen 6 449 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception name for a flagged stop Skaperen 1 542 May-31-2019, 04:33 AM
Last Post: heiner55
  name of exception for exit() Skaperen 2 248 May-24-2019, 07:07 PM
Last Post: Skaperen
  exception handling KyawMyo 3 272 May-07-2019, 07:53 AM
Last Post: buran
  Stack trace shows different exception type than print micseydel 5 545 Apr-01-2019, 10:24 PM
Last Post: micseydel
  Database operation exception handling LostInCode 1 341 Jan-03-2019, 07:50 PM
Last Post: jeanMichelBain

Forum Jump:


Users browsing this thread: 1 Guest(s)