Python: Function inside IF. Problems

Viewed 59

I am new to Python. I'm having trouble executing an IF with a function inside. I enclose the code, including the graphic interface in Tkinter. In the same algorithm, I was wrong and i have difficulty in 2 things: write the correct IF conditions, and correctly call the Function inside IF, that is:

if categorias == "Phrase of love" and sottocategorias == "Short sentences":
     phrase_example

(it's written with errors, I know)

I would like to do that: if in the combobox1 (Categorys) I select the "Phrase of love" item, and then in the combobox2 (Subcategorias) I select the "Short sentences" item (selected both, at the same time, from the two comboboxes) ... then I click on the button and the Function is called (the code has already been written). The task of the Function, as you can see, is to print sentences (from the database) in the multiline textbox.

I looked for other similar questions to avoid asking questions, but didn't understand much considering I just got started with Python. Can you show me where I'm wrong by showing me the correct IF code and Function code? Thank you

from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3

window=Tk()
window.title("Test")
window.geometry("700x700")
window.configure(bg='#78c030')

### BUTTON ###
def phrase_example(): # setting this to button 2
    cursor.execute('SELECT phrase2, phrase3 FROM allphrase ORDER BY RANDOM() LIMIT 
1')
    word2,word3 = cursor.fetchone()
    text.delete(1.0,END)
    text.insert(tk.END,f"{word2} {word3}")

if categorias == "Phrase of love" and sottocategorias == "Short sentences":
     phrase_example

button2 = Button(window, text="Button2", bg='white', command = phrase_example)
button2.pack()
button2.place(x=5, y=330)

### TEXTBOX MULTILINE ###
text = Text(window,width=63,height=38)
text.pack()
text.place(x=180, y=24)

### CATEGORIA E SOTTOCATEGORIA ###
cat=StringVar()
sub_cat=StringVar()

def change_val(*args):
    if cat.get() == "Phrase of love":
        sottocategorias = ["Short sentences", "Long sentences", "Medium sentences"]
        sottocategoria.config(values=sottocategorias)
    else:
        sottocategorias = ["aaaa"]
        sottocategoria.config(values=sottocategorias)
    
categorias=["Phrase of love", "Phrase of life", "Phrase of spiritual"]
categoria=ttk.Combobox(window,value=categorias,textvariable=cat,width=16)
categoria.place(x=5, y=25)
cat.set("Scegliere categoria")

sottocategorias=["aaaa"]
sottocategoria=ttk.Combobox(window,textvariable=sub_cat,value=sottocategorias,
width=16)
sottocategoria.place(x=5, y=55)
cat.trace("w",change_val)

con = sqlite3.connect('/home/mypc/Scrivania/Test/Database.db')
cursor = con.cursor()

window.mainloop()
1 Answers

You can do the IF checking inside phrase_example().

Also you need to use categoria.get() == ... and sottocategoria.get() == ... instead of categorias == ... and sottocategorias == ...:

def phrase_example():
    if categoria.get() == "Phrase of love" and sottocategoria.get() == "Short sentences":
        cursor.execute('SELECT phrase2, phrase3 FROM allphrase ORDER BY RANDOM() LIMIT 1')
        word2,word3 = cursor.fetchone()
        text.delete(1.0,END)
        text.insert(tk.END,f"{word2} {word3}")
Related