0

I'm having some trouble with my code. And can't seem to figure out a solution to my issue.

I'm adding Tkinter to a piece of code I made and can't seem to find a way to recreate using inputs.

I've used inputs to store answers from a user as such:

answer1 = input('Put your first answer here: ')
answer2 = input('Put your second answer here: ')
answer3 = input('Put your third answer here: ')          
answer1 = answer1.lower()
answer2 = answer2.lower()
answer3 = answer3.lower()

Now I need to use a Tkinter entry to recreate it but i can't figure it out. I've tried buttons to read the text, store it and then clear the entry but i couldn't get it to increment as I wanted. But I've hit a roadblock and would appreciate some help.

Many thanks in advance.

Full code:

import tkinter as tk
from tkinter import *



import sqlite3
def readquestions(questionnumber):
    with sqlite3.connect("Coursework database.db") as db:
        cursor = db.cursor()

        cursor.execute("SELECT * From Questions WHERE QuestionID = ?",
                       (str(questionnumber)))

        global questiontext
        rows = cursor.fetchall()
        customername1 = ''
        for row in rows:  
            print(str(row[1]))
            questiontext = str(row[1])
        return questiontext


def GUIcolourwindows():
    Changecolour = tk.Tk()
    Changecolour.title('Colour picker!')
    Changecolour.geometry('300x300')
    Changecolour.configure(background='grey')
    addbtn = Button(Changecolour, text = 'Green', bg = 'green', fg = 'white', command = changecolourgreen)
    addbtn.place(x = 00, y = 00, width = 100, height = 100)
    addbtn = Button(Changecolour, text = 'Red', bg = 'red', command = changecolourred)
    addbtn.place(x = 100, y = 00, width = 100, height = 100)
    addbtn = Button(Changecolour, text = 'Pink', bg = 'pink', command = changecolourpink)
    addbtn.place(x = 200, y = 0, width = 100, height = 100)
    addbtn = Button(Changecolour, text = 'White', bg = 'white', command = changecolourwhite)
    addbtn.place(x = 00, y = 100, width = 100, height = 100)
    addbtn = Button(Changecolour, text = 'Grey', command = changecolourgrey)
    addbtn.place(x = 100, y = 100, width = 100, height = 100)
    addbtn = Button(Changecolour, text = 'Blue', bg = 'blue', fg = 'white', command = changecolourblue)
    addbtn.place(x = 200, y = 100, width = 100, height = 100)
    addbtn = Button(Changecolour, text = 'Black', bg = 'black', fg = 'white', command = changecolourblack)
    addbtn.place(x = 00, y = 200, width = 100, height = 100)
    addbtn = Button(Changecolour, text = 'Orange', bg = 'orange', command = changecolourorange)
    addbtn.place(x = 100, y = 200, width = 100, height = 100)    
    addbtn = Button(Changecolour, text = 'Purple', bg = 'purple', fg = 'white', command = changecolourpurple)
    addbtn.place(x = 200, y = 200, width = 100, height = 100)


def changecolourgreen():
    colour = 'green'
    GUIwindow(colour)

def changecolourred():
    colour = 'red'
    GUIwindow(colour)

def changecolourpink():
    colour = 'pink'
    GUIwindow(colour)

def changecolourwhite():
    colour = 'white'
    GUIwindow(colour)

def changecolourgrey():
    colour = 'grey'
    GUIwindow(colour)

def changecolourblue():
    colour = 'blue'
    GUIwindow(colour)

def changecolourblack():
    colour = 'black'
    GUIwindow(colour)

def changecolourorange():
    colour = 'orange'
    GUIwindow(colour)

def changecolourpurple():
    colour = 'purple'
    GUIwindow(colour)

def GUIwindow(colour):
    questionnumber = 1
    readquestions(questionnumber)

    Codingforkids = tk.Tk()
    Codingforkids.title('Coding for kids')
    Codingforkids.geometry('600x800')
    Codingforkids.configure(background=colour)

    Questionarea = Label(Codingforkids, text = questiontext, bg = 'white',fg = 'black', justify = 'left')
    Questionarea.place(x = 50, y = 50, width = 500, height = 500)
    Answerarea = Entry(Codingforkids, text = 'Enter your answer here:', bg = 'white',fg = 'black')
    Answerarea.place(x = 50, y = 600, width = 500, height = 150)
    Answerbtn = Button(Codingforkids, text = 'Submit answer', bg = 'light grey', fg = 'black', command =  Gettext)
    Answerbtn.place(x = 450, y = 600, width = 100, height = 150)


def Gettext():
    answer = text.get("1.0","end-1c")
    print(answer)
    return answer

GUIcolourwindows()

1 Answers1

0

That was a lot of code. Please provide a Minimal, Complete, and Verifiable example.

I did however notice one thing that usually breaks a GUI: you have created two instances of Tk, once in GUIcolourwindows() and once in GUIwindow(colour). See Why are multiple instances of Tk discouraged?

figbeam
  • 7,001
  • 2
  • 12
  • 18