Funktionierender Code - nicht funktionierender Code..
Um Redundanzen zu vermeiden wollte ich umstellen - und hab 'nen Hänger..
Was -soweit fertig geschrieben- läuft:
Code: Alles auswählen
import tkinter as tk
from tkinter import *
from tkinter import font
import random
import sqlite3
class DBConnection():
def __init__(self, host):
self.connection = None
self.host = host
def __enter__(self):
self.connection = sqlite3.connect(host)
return self.connection
def __exit__(self, exc_type, exc_val, exc_tb):
self.connection.commit()
self.connection.close()
# create database
def createBase():
try:
with DBConnection('data.db') as connection:
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS done(ergebnis integer primary key)')
except:
print('An error occured.')
# insert data into database
def insertBase():
try:
with DBConnection('data.db') as connection:
cursor = connection.cursor()
cursor.execute(f'INSERT INTO data VALUES(?)', (ergebnis))
except:
print('An error occured.')
# extracting data from table
def selectBase():
try:
with DBConnection('data.db') as connection:
cursor = connection.cursor()
cursor.execute('SELECT * FROM done WHERE name=?', (name))
except:
print('An error occured.')
def deleteBase():
try:
with DBConnection('data.db') as connection:
cursor = connection.cursor()
cursor.execute('DELETE * FROM done WHERE name=?', (name))
except:
print('An error occured.')
font = ('Helvetica', 14)
Font = ('Helvetica', 28)
FONT = ('Helvetica', 48)
class Maths4JoyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
window = tk.Frame(self)
window.pack(side='top', fill='both', expand = True)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (MainWindow, AdditionWindow, SubtraktionWindow, MultiplikationWindow, DivisionWindow):
frame = F(window, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='NEWS')
self.show_frame(MainWindow)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class MainWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
welbutton = Button(self, width='45', height='3', text='Anmelden', bg='gray', fg='white', font=font, command=LoginWindow).place(x=220, y=32)
addbutton = Button(self, width='20', height='5', text='Addition', bg='yellow', fg='blue', font=font, command=lambda :controller.show_frame(AdditionWindow)).place(x=220, y=130)
subbutton = Button(self, width='20', height='5', text='Subtraktion', bg='green', fg='red', font=font, command=lambda :controller.show_frame(SubtraktionWindow)).place(x=495, y=130)
mulbutton = Button(self, width='20', height='5', text='Multiplikation', bg='red', fg='green', font=font, command=lambda :controller.show_frame(MultiplikationWindow)).place(x=220, y=275)
divbutton = Button(self, width='20', height='5', text='Division', bg='blue', fg='yellow', font=font, command=lambda :controller.show_frame(DivisionWindow)).place(x=495, y=275)
savbutton = Button(self, width='45', height='3', text='Speichern', bg='gray', fg='white', font=font).place(x=220, y=420)
class AdditionWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def checkAdd():
if control.get() != ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=NOPE)
elif control.get() == ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=YES)
else:
checkField.create_image(140, 135, anchor=CENTER, image=LOGO)
NOPE = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_red.gif')
LOGO = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/wolfsw.gif')
YES = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_green.gif')
operand = random.randint(1, 99999)
operator = random.randint(1, 99999)
ergebnis = operand + operator
checkField = Canvas(self, width=265, height=267, bg='silver')
checkField.place(x=630, y=260)
backbutton = Button(self, width=10, height=1, text='Menue', bg='red',fg='blue', font=font, command=lambda :controller.show_frame(MainWindow)).place(x=10, y=10)
repeatbutton = Button(self, width=20, height=1, text='neue Aufgabe', bg='green',fg='yellow', font=font, command=lambda :controller.show_frame(createNew)).place(x=365, y=10)
skipbutton = Button(self, width=10, height=1, text='Subtraktion', bg='blue',fg='red', font=font).place(x=820, y=10)
operandFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operandFeld.place(x=75, y=125)
operandFeld.insert(INSERT, operand)
plusSign = Label(self, width=1, height=1, text='+', bg='silver', fg='red', font=FONT).place(x=270, y=125)
operatorFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operatorFeld.place(x=325, y=125)
operatorFeld.insert(INSERT, operator)
equalSign = Label(self, width=1, height=1, text='=', bg='silver', fg='green', font=FONT).place(x=520, y=125)
control = IntVar()
eingabe = Entry(self, width=6, bg='white', fg='blue', font=FONT, textvariable=control).place(x=575, y=125)
sendButton = Button(self, width=3, height=1, relief=RAISED, text='OK', bg='khaki', fg='black', font=Font, command=checkAdd).place(x=825, y=127)
nebenlabel = Label(self, text='Mein Platz für Nebenrechnungen', width=39, bg='orange', fg='blue').place(x=10, y=260)
nebenrechnung = Text(self, width=25, height=11, bg='white', fg="orange", font=font).place(x=10, y=285)
class SubtraktionWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def checkMinus():
if control.get() != ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=NOPE)
elif control.get() == ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=YES)
else:
checkField.create_image(140, 135, anchor=CENTER, image=LOGO)
NOPE = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_red.gif')
LOGO = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/wolfsw.gif')
YES = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_green.gif')
operand = random.randint(1, 99999)
operator = random.randint(1, 9999)
ergebnis = operand - operator
checkField = Canvas(self, width=265, height=267, bg='silver')
checkField.place(x=630, y=260)
backbutton = Button(self, width=10, height=1, text='Menue', bg='red',fg='blue', font=font, command=lambda :controller.show_frame(MainWindow)).place(x=10, y=10)
repeatbutton = Button(self, width=20, height=1, text='neue Aufgabe', bg='green',fg='yellow', font=font, command=lambda :controller.show_frame(SubtraktionWindow)).place(x=365, y=10)
skipbutton = Button(self, width=10, height=1, text='Subtraktion', bg='blue',fg='red', font=font).place(x=820, y=10)
operandFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operandFeld.place(x=75, y=125)
operandFeld.insert(INSERT, operand)
minusSign = Label(self, width=1, height=1, text='-', bg='silver', fg='red', font=FONT).place(x=270, y=125)
operatorFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operatorFeld.place(x=325, y=125)
operatorFeld.insert(INSERT, operator)
equalSign = Label(self, width=1, height=1, text='=', bg='silver', fg='green', font=FONT).place(x=520, y=125)
control = IntVar()
eingabe = Entry(self, width=6, bg='white', fg='blue', font=FONT, textvariable=control).place(x=575, y=125)
sendButton = Button(self, width=3, height=1, relief=RAISED, text='OK', bg='khaki', fg='black', font=Font, command=checkMinus).place(x=825, y=127)
nebenlabel = Label(self, text='Mein Platz für Nebenrechnungen', width=39, bg='orange', fg='blue').place(x=10, y=260)
nebenrechnung = Text(self, width=25, height=11, bg='white', fg="orange", font=font).place(x=10, y=285)
class MultiplikationWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def checkMulti():
if control.get() != ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=NOPE)
elif control.get() == ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=YES)
else:
checkField.create_image(140, 135, anchor=CENTER, image=LOGO)
NOPE = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_red.gif')
LOGO = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/wolfsw.gif')
YES = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_green.gif')
operand = random.randint(1, 99999)
operator = random.randint(1, 9999)
ergebnis = operand * operator
checkField = Canvas(self, width=265, height=267, bg='silver')
checkField.place(x=630, y=260)
backbutton = Button(self, width=10, height=1, text='Menue', bg='red',fg='blue', font=font, command=lambda :controller.show_frame(MainWindow)).place(x=10, y=10)
skipbutton = Button(self, width=10, height=1, text='Subtraktion', bg='blue',fg='red', font=font).place(x=820, y=10)
operandFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operandFeld.place(x=75, y=125)
operandFeld.insert(END, operand)
multiSign = Label(self, width=1, height=1, text='x', bg='silver', fg='red', font=FONT).place(x=270, y=125)
operatorFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operatorFeld.place(x=325, y=125)
operatorFeld.insert(END, operator)
equalSign = Label(self, width=1, height=1, text='=', bg='silver', fg='green', font=FONT).place(x=520, y=125)
control = IntVar()
eingabe = Entry(self, width=6, bg='white', fg='blue', font=FONT, textvariable=control).place(x=575, y=125)
sendButton = Button(self, width=3, height=1, relief=RAISED, text='OK', bg='khaki', fg='black', font=Font, command=checkMulti).place(x=825, y=127)
nebenlabel = Label(self, text='Mein Platz für Nebenrechnungen', width=39, bg='orange', fg='blue').place(x=10, y=260)
nebenrechnung = Text(self, width=25, height=11, bg='white', fg="orange", font=font).place(x=10, y=285)
def fresh_frame():
operandFeld.delete(1.0, END)
operatorFeld.delete(1.0, END)
operand = random.randint(1, 99999)
operator = random.randint(1, 9999)
operandFeld.insert(INSERT, operand)
operatorFeld.insert(INSERT, operator)
repeatbutton = Button(self, width=20, height=1, text='neue Aufgabe', bg='green',fg='yellow', font=font, command=fresh_frame).place(x=365, y=10)
class DivisionWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def checkDivide():
if control.get() != ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=NOPE)
elif control.get() == ergebnis:
checkField.create_image(140, 135, anchor=CENTER, image=YES)
else:
checkField.create_image(140, 135, anchor=CENTER, image=LOGO)
class buildNums():
num1 = random.randint(1, 99999)
num2 = random.randint(1, 9999)
while num1 % num2 != 0:
num1 = random.randint(1, 99999)
num2 = random.randint(1, 9999)
operand = buildNums.num1
operator = buildNums.num2
ergebnis = operand / operator
NOPE = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_red.gif')
LOGO = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/wolfsw.gif')
YES = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_green.gif')
checkField = Canvas(self, width=265, height=267, bg='silver')
checkField.place(x=630, y=260)
backbutton = Button(self, width=10, height=1, text='Menue', bg='red',fg='blue', font=font, command=lambda :controller.show_frame(MainWindow)).place(x=10, y=10)
repeatbutton = Button(self, width=20, height=1, text='neue Aufgabe', bg='green',fg='yellow', font=font, command=lambda :controller.show_frame(buildNums)).place(x=365, y=10)
skipbutton = Button(self, width=10, height=1, text='Subtraktion', bg='blue',fg='red', font=font).place(x=820, y=10)
operandFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operandFeld.place(x=75, y=125)
operandFeld.insert(INSERT, operand)
divideSign = Label(self, width=1, height=1, text=':', bg='silver', fg='red', font=FONT).place(x=270, y=125)
operatorFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operatorFeld.place(x=325, y=125)
operatorFeld.insert(INSERT, operator)
equalSign = Label(self, width=1, height=1, text='=', bg='silver', fg='green', font=FONT).place(x=520, y=125)
control = IntVar()
eingabe = Entry(self, width=6, bg='white', fg='blue', font=FONT, textvariable=control).place(x=575, y=125)
sendButton = Button(self, width=3, height=1, relief=RAISED, text='OK', bg='khaki', fg='black', font=Font, command=checkDivide).place(x=825, y=127)
nebenlabel = Label(self, text='Mein Platz für Nebenrechnungen', width=39, bg='orange', fg='blue').place(x=10, y=260)
nebenrechnung = Text(self, width=25, height=11, bg='white', fg="orange", font=font).place(x=10, y=285)
class LoginWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
login = self.login = tk.Toplevel()
login.title('LogIn')
login.geometry('320x180+66+33')
login.grid_rowconfigure(0, weight=1)
login.grid_columnconfigure(0, weight=1)
NameL = Label(self, text='Anmeldename: ').pack()
Name = IntVar()
User = Entry(login, text='Anmeldename', textvariable=Name).place(x=175, y=55)
PWL = Label(self, text='Passwort: ').place(x=50, y=75)
PW = IntVar()
Data = Entry(login, text='Passwort', textvariable=PW).place(x=175, y=75)
LogButton = Button(login, text='Anmelden',).grid(row=5, column=0, sticky='W')
CreaButton = Button(login, text='Registrieren').grid(row=5, column=1, sticky='E')
app = Maths4JoyApp()
app.title('maths4Joy')
app.geometry('960x540+200+100')
app.configure(bg='silver')
app.resizable(0, 0)
myMenu = Menu(app)
myMenu.add_command(label='Meine Ergebnisse')
myMenu.add_command(label='Ende', command=app.quit)
myMenu.add_command(label='Löschen')
app.config(menu=myMenu)
app.mainloop()
Code: Alles auswählen
import tkinter as tk
from tkinter import *
from tkinter import font
import random
# setting up fonts S, M, L
font = ('fontetica', 14)
Font = ('fontetica', 28)
FONT = ('fontetice', 48)
class Maths4JoyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
window = tk.Frame(self)
window.pack(side='top', fill='both', expand = True)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (MainWindow, OperationWindow):
frame = F(window, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='NEWS')
self.show_frame(MainWindow)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class MainWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
welbutton = Button(self, width='45', height='3', text='Anmelden', bg='gray', fg='white', font=font, command=LoginWindow).place(x=220, y=32)
addition = Button(self, width='20', height='5', text='Addition', bg='yellow', fg='blue', font=font, command=lambda : controller.show_frame(OperationWindow)).place(x=220, y=130)
subtraction = Button(self, width='20', height='5', text='subtraction', bg='green', fg='red', font=font, command=lambda :controller.show_frame(OperationWindow)).place(x=495, y=130)
multiplication = Button(self, width='20', height='5', text='Multiplikation', bg='red', fg='green', font=font, command=lambda :controller.show_frame(OperationWindow)).place(x=220, y=275)
division = Button(self, width='20', height='5', text='Division', bg='blue', fg='yellow', font=font, command=lambda :controller.show_frame(OperationWindow)).place(x=495, y=275)
savbutton = Button(self, width='45', height='3', text='Speichern', bg='gray', fg='white', font=font).place(x=220, y=420)
class OperationWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
NOPE = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_red.gif')
LOGO = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/wolfsw.gif')
YES = PhotoImage(file='C:/Users/User/OneDrive/_GitHub/maths4joy/images/s_green.gif')
def addition(self):
sign = Label(width=1, height=1, text='+', bg='silver', fg='red', font=FONT).place(x=270, y=125)
num1 = random.randint(1, 99999)
num2 = random.randint(1, 99999)
num3 = (num1 + num2)
return (sign, num1, num2, num3)
def subtraction(self):
sign = Label(width=1, height=1, text='-', bg='silver', fg='red', font=FONT).place(x=270, y=125)
num1 = random.randint(1, 99999)
num2 = random.randint(1, 99999)
if num1 < num2:
num1 = random.randint(1, 99999)
num2 = random.randint(1, 99999)
num3 = (num1 - num2)
return num1, num2, num3
def multiplication(self):
sign = Label(width=1, height=1, text='x', bg='silver', fg='red', font=FONT).place(x=270, y=125)
num1 = random.randint(1, 99999)
num2 = random.randint(1, 999)
num3 = (num1 * num2)
return num1, num2, num3
def division(self):
sign = Label(width=1, height=1, text=':', bg='silver', fg='red', font=FONT).place(x=270, y=125)
while num1 % num2 != 0:
num1 = random.randint(1, 99999)
num2 = random.randint(1, 9999)
num3 = (num1 / num2)
return num1, num2, num3
operand = num1.get()
operator = num2.get()
result = num3.get()
def checkReturn():
if control.get() != result.get():
checkField.create_image(140, 135, anchor=CENTER, image=NOPE)
elif control.get() == result.get():
checkField.create_image(140, 135, anchor=CENTER, image=YES)
else:
checkField.create_image(140, 135, anchor=CENTER, image=LOGO)
checkField = Canvas(self, width=265, height=267, bg='silver')
checkField.place(x=630, y=260)
backbutton = Button(self, width=10, height=1, text='Menue', bg='red',fg='blue', font=font, command=lambda :controller.show_frame(MainWindow)).place(x=10, y=10)
skipbutton = Button(self, width=10, height=1, text='subtraction', bg='blue',fg='red', font=font).place(x=820, y=10)
operandFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operandFeld.place(x=75, y=125)
operandFeld.insert(END, operand)
operatorFeld = Text(self, width=5, height=1, bg='white', fg='blue', font=FONT)
operatorFeld.place(x=325, y=125)
operatorFeld.insert(END, operator)
equalSign = Label(self, width=1, height=1, text='=', bg='silver', fg='green', font=FONT).place(x=520, y=125)
control = IntVar()
eingabe = Entry(self, width=6, bg='white', fg='blue', font=FONT, textvariable=control).place(x=575, y=125)
sendButton = Button(self, width=3, height=1, relief=RAISED, text='OK', bg='khaki', fg='black', font=Font, command=checkReturn).place(x=825, y=127)
nebenlabel = Label(self, text='Mein Platz für Nebenrechnungen', width=39, bg='orange', fg='blue').place(x=10, y=260)
nebenrechnung = Text(self, width=25, height=11, bg='white', fg="orange", font=font).place(x=10, y=285)
def fresh_frame():
operandFeld.delete(1.0, END)
operatorFeld.delete(1.0, END)
operand = random.randint(1, 99999)
operator = random.randint(1, 9999)
operandFeld.insert(END, operand)
operatorFeld.insert(END, operator)
repeatbutton = Button(self, width=20, height=1, text='neue Aufgabe', bg='green',fg='yellow', font=font, command=fresh_frame).place(x=365, y=10)
class LoginWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
login = self.login = tk.Toplevel()
login.title('LogIn')
login.geometry('320x180+66+33')
login.grid_rowconfigure(0, weight=1)
login.grid_columnconfigure(0, weight=1)
NameL = Label(self, text='Anmeldename: ').pack()
Name = IntVar()
User = Entry(login, text='Anmeldename', textvariable=Name).place(x=175, y=55)
PWL = Label(self, text='Passwort: ').place(x=50, y=75)
PW = IntVar()
Data = Entry(login, text='Passwort', textvariable=PW).place(x=175, y=75)
LogButton = Button(login, text='Anmelden',).grid(row=5, column=0, sticky='W')
CreaButton = Button(login, text='Registrieren').grid(row=5, column=1, sticky='E')
app = Maths4JoyApp()
app.title('maths4Joy')
app.geometry('960x540+200+100')
app.configure(bg='silver')
app.resizable(0, 0)
myMenu = Menu(app)
myMenu.add_command(label='Meine resultse')
myMenu.add_command(label='Ende', command=app.quit)
myMenu.add_command(label='Löschen')
app.config(menu=myMenu)
app.mainloop()
# Nachtrag: ..gerade gemerkt - läuft nicht (deuglyfied), liegt daran, dass.. ------------------------------------------------------------------------------------------------------------------- #
# 1. ..hatte ich erfolglos versucht die Buttons mit einem "command=lambda :controller.show_frame(OperationWindow.~method)" zu belegen ------ #
# 2. ..hatte ich über den def's die nums definiert als num1 = {} / [] / () num2 = ... ----------------------------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Für Anregungen wäre ich sehr dankbar.
Schönes Wochenende!