Issue with frame closing Tkinter - python-3.x

I am having a issue with my frame switching after I click a button for my results. My button is hooked up to for inserting the fields into a database and it does do that but after it does it pulls the login screen back to the toplevel of the screen and just stay on the same screen how can I prevent this from happening? I am thinking its happening once the db closes. I don't get any errors.
if self.first == "":
ms.showerror('Please enter a first name')
elif self.last == "":
ms.showerror('Please enter a last name')
elif self.date == "":
ms.showerror('Please enter a username')
elif self.addr == "":
ms.showerror('Please enter a password')
elif self.phoneNum == "":
ms.showerror('Please enter a password')
else:
findSql = 'SELECT firstname, lastname FROM client WHERE firstname =? and
lastname=? '
c.execute(findSql, [(self.first),(self.last)])
f = c.fetchall()
if f:
ms.showerror('User already exist')
else:
ms.showinfo('Account has been created')
self.insert = 'INSERT INTO client(firstname, lastname, birthdate,
address, phone, symptom1, symptom2, symptom3, symptom4, symptom5)
VALUES(?,?,?,?,?,?,?,?,?,?)'
c.execute(self.insert,[self.first, self.last, self.date, self.addr,
self.phoneNum, self.sym1, self.sym2, self.sym3, self.sym4, self.sym5])
db.commit()
db.close()
# runs calculation accuracy for random forest
a = Algo()
a.randomForest(self.master,self.Symptom1, self.Symptom2, self.Symptom3, self.Symptom4,self.Symptom5)

Related

i want to print dictionary keys and values separately with given string in python for my project

I'm not able to finish the project given below if someone help me with the particular function paasenger_login ...help me out
The project is flight reservation system with basic pyhton programming
a. Passenger
1. sign in
2.check availability and fare
3.book ticket
b. Cashier
1.approve
2.issue ticket
the code is below
s=[]
class passenger:
pass
class book:
d={}
f={}
pact=1-000-000
new=0
class cashier:
def caslogin():
while True:
print("\t1.Filling Tickets\n\t2.passenger Info\n\t3.Approve Tickets\n\t4.Logout")
x=input()
if x=='1':
if (book.new==0):
flight=int(input("Enter the no of flights: "))
for i in range(flight):
print("Enter name of the flights: ")
q=input()
print("Enter the price for to travel:\n")
op=[]
op.append(input())
book.d[q]=op
print("Successfully loaded:\n")
book.new=1
else:
print("\n\tAlready loaded")
elif x=='2':
if book.pact<=1-000-000:
print("NO user found!................")
else:
for i in range(len(s)):
print(s[i].name,i+1-000-000,s[i].history,s[i].report)
elif x=='3':
if book.pact<=1-000-000:
print("NO record found!................")
else:
for i in s:
print(i.name,i.report)
else:
break
def passenger_login(rn):
while True:
print("Successfully logged in... \n\t1.Book Tickets\n\t2.Approval\n\t3.Logout")
n=input()
if n=='1':
if len(book.d):
for i in book.d:
print(str(i),str(book.d[i]))
else:
break
def __main__():
print("Air Asia\n")
while True:
print("Login as\n\t1.Cashier\n\t2.passenger\n\t3.Exit")
n=input()
if n=='1':
cashier.caslogin()
elif n=='2':
while True:
print("Passenger Info\n\t1.Sign in\n\t2.Sign up\n\t3.Logout")
x=input()
if x=='1':
try:
reg=int(input("Enter the serial no:\n"))
Pass=input("Enter the password:\n")
except:
print("Enter the serial no:")
try:
reg=int(input("Enter the serial no:\n"))
Pass=input("Enter the password:\n")
except:
print("Try after few minutes")
if reg != len(s):
print("NO account found")
else:
if s[reg-1-000-000].ps==Pass:
passenger_login(reg-1-000-000)
elif x=='2':
s.append(passenger())
s[book.pact - 1-000-000].name=input("Enter your name:\n")
s[book.pact - 1-000-000].ps=input("create password:\n")
s[book.pact - 1-000-000].history=0
s[book.pact - 1-000-000].report="New user"
print("Your serial no is : ",book.pact)
book.pact+=1
else:
break
else:
break
__main__()

Code not being executed, program jumps out of the loop and waits for another command

I'm trying to return 'search not found' when nothing is found in the database, but the program just jumps out the loop
I tried checking for 0 or '', but realized that the database returns NoneType. I'm checking for None now, but the code still does not get executed.
def search():
def searchmenu():
print('')
ans=str(input('Make another [s]earch or return to the [m]ain menu: '))
ans=ans.lower()
if ans=='s':
Inventory.search()
elif ans=='m':
menu=MainMenu()
menu.menuops()
else:
print('invalid menu option')
searchmenu()
mydb = mysql.connector.connect(host="localhost", user="root", password="", database="pharmd")
cursor = mydb.cursor()
query=str(input("Search: "))
if query != "":
cursor.execute('SELECT * FROM drugs WHERE name LIKE "%'+query+'%"')
results=cursor.fetchall()
if results != None:
for data in results:
print('found!')
print(data)
searchmenu()
else:
print('No results found')
else:
print('Please enter a valid search!')
print('')
Inventory.search()
The problem is that cursor.fetchall() doesn't return None but an empty tuple ().
You can check cursor.rowcount for empty result according to the answer for this question.

Python script placed in /usr/bin not saving data in sqlite db

I wrote a program which gets username and password from the user and creates a table in an SQLite DB then sends those data through a connection to DB.
It was working fine until I added the shebang in my code, then I made it executable(chmod +x) and after all, I put that file into /usr/bin so that I could use it anywhere in the terminal. now it runs but it doesn't work (it doesn't add data to DB).
here is my code:
#!/usr/bin/python3
__author__ = "Taha Jalili"
__license__ = "GPL"
__version__ = "1.0.0"
__email__ = "tahajalili#gmail.com"
import sys
import sqlite3
from sqlite3 import *
import inquirer
SQL_CREATE_STATEMENT = '''CREATE TABLE password
(id integer PRIMARY KEY NOT NULL,username text, password text, source text)'''
SQL_INSERT_STATEMENT = '''INSERT INTO password (username, password, source)VALUES(?,?,?)'''
DATABASE_PATH = '/home/taha/lessons/projects/passStorage/passDB.db'
def create_connection(db_file):
try:
conn = sqlite3.connect(db_file)
return conn
print('Connection created.')
except Error as e:
return e
# def create_table(connection, sql_commands):
# c = connection.cursor()
# c.execute(sql_commands)
# print('=> Table created.')
def get_input():
USERNAME = input('username: ')
PASSWORD = input('password: ')
SOURCE = input('source: ')
return USERNAME,PASSWORD,SOURCE
def insert_date(connection,data):
try:
c = connection.cursor()
c.execute(SQL_INSERT_STATEMENT, data)
print('=> Data insertion done.')
except Error as e:
return e
def show_info(connection):
c = connection.cursor()
info = c.execute('SELECT * FROM password ORDER BY id')
print('YOUR PASSWORDS'.center(45,'-'))
print('(id, username, password, source)')
for row in info:
print(row,'')
print('\n')
def ask_again():
user_choice = input("Wish to continue? Y/N ")
if user_choice == 'y' or user_choice == 'Y':
main()
elif user_choice == 'n' or user_choice == 'N':
print("==> BYE <==")
sys.exit(0)
def main():
conn = create_connection(DATABASE_PATH)
# create_table(conn, SQL_CREATE_STATEMENT)
questions = [
inquirer.List(
'job',
message = 'What should I do?',
choices=['Add data','Show saved data.']
),
]
answers = inquirer.prompt(questions)
if answers['job'] == 'Add data':
conn2 = create_connection(DATABASE_PATH)
insert_date(conn2,get_input())
elif answers['job'] == 'Show saved data.':
show_info(conn)
ask_again()
conn.commit()
conn.close()
if __name__ == '__main__':
main()

Python code [Code loops]

Hello am very new to python and ive atempted my first code like this but something seems to be wrong and one of the steps keeps looping. I am very confused on what to do so could someone please help me out.
Thank you!
import os
import time
def main():
while True:
print("Welcome To Amazon")
search = input("Search.... ")
if 'search == registration':
reg()
if 'search == login':
login()
#Must Register to continue
def reg():
while True:
print("Display Name")
reg_user = input()
print("Password")
reg_pass = input()
def registered():
time.sleep(1)
print("Registration Successful!")
main()
#Must Login to continue
def login():
while True:
print("Enter Username: ")
username = input()
print("Enter Password: ")
password = input()
if 'username == reg_user' and 'password == reg_pass':
time.sleep(1)
print("Login Successful!")
logged()
else:
print("Try Again!")
def logged():
time.sleep(1)
print("Welcome To CityRP Gaming")
main()
The while loop loops for as long as the condition is true. You used While True, and True will always be True. This means the loop will continue forever. To break out of a loop you can use 'break'.

Running python on server, executing commands from computer

I made a login system with python. It works perfectly, but i want to run script on server or web. For example: Steam. Steam wants username and password to log in. So i wanted to do the same for my script. How can i do that?
My Code:
import os
import string
import time
version = "1.0 Alfa"
def login():
print ("----------------------------------------")
print (" Login ")
print ("----------------------------------------")
k_name = input("Enter username: ")
if os.path.exists(k_name + ".txt") == False:
print ("Username not found.")
create()
else:
k_pass = input("Enter password: ")
with open(k_name + ".txt", "r") as f:
if k_pass == f.read():
print("Welcome %s!"%k_name)
f.close()
input()
else:
print("Password is wrong!")
create()
def create():
print("You using login system %s" % version)
print( "----------------------------------------")
print("| Lobby |")
print( "----------------------------------------")
starting = input("To create user type R, to login type L").upper()
if starting == "R":
name = input("Enter username: ")
password = input("Enter password: ")
password2 = input("Enter password again: ")
if password == password2:
newfile = open(name + ".txt", "w")
newfile.write(password)
newfile.close()
print("User created. Redirecting you to login.")
time.sleep(2)
login()
elif password != password2:
print("Passwords doesn't match.")
input()
create()
elif starting == "L":
login()
else:
print("\nWrong button\n")
create()
create()
Here is the script which you can run : python test.py user pass
it will save data if file not found and perform login
#!/usr/bin/env python
import sys, getopt
import os
import time
version = "1.0 Alfa"
def login(username=None, password=None):
print ("----------------------------------------")
print (" Login ")
print ("----------------------------------------")
if username:
k_name = username
else:
k_name = input("Enter username: ")
if os.path.exists(k_name + ".txt") == False:
print ("Username not found.")
create(username, password, "R")
else:
if password:
k_pass = password
else:
k_pass = input("Enter password: ")
with open(k_name + ".txt", "r") as f:
if k_pass == f.read():
if not username:
print("Welcome %s!"%k_name)
f.close()
input()
else:
print("Password is wrong!")
create()
def create(username=None, password=None, mode="L"):
print("You using login system %s" % version)
print( "----------------------------------------")
print("| Lobby |")
print( "----------------------------------------")
if mode:
starting = mode
else:
starting = input("To create user type R, to login type L").upper()
if starting == "R":
if username:
name = username
else:
name = input("Enter username: ")
if password:
password2 = password
else:
password = input("Enter password: ")
password2 = input("Enter password again: ")
if password == password2:
newfile = open(name + ".txt", "w")
newfile.write(password)
newfile.close()
print("User created. Redirecting you to login.")
time.sleep(2)
login(username, password)
elif password != password2:
print("Passwords doesn't match.")
input()
create()
elif starting == "L":
login(username, password)
else:
print("\nWrong button\n")
create()
def main(argv):
print sys.argv
if len(sys.argv) < 3:
print 'test.py <username> <password>'
sys.exit()
username = sys.argv[1]
password = sys.argv[2]
print 'username is ', username
print 'password is ', password
create(username, password)
if __name__ == "__main__":
main(sys.argv[1:])

Resources