Home > front end >  The random function does not start correctly. If I click on the button the text doesn't change,
The random function does not start correctly. If I click on the button the text doesn't change,

Time:07-27

This script has the purpose of displaying the word "della gara" or "del match" in combination with "valida" or "valido, depending on the grammar rule if male or female. Don't worry, it works correctly :) Don't worry, even if the text is in a non-English language, it doesn't matter, because it prints quietly. All that is needed is to apply the random function with sinonimo_partita_random inside the for loop.

PROBLEM: The problem is that the random function doesn't start correctly when I click the Print button. Try clicking on the Print button many times, nothing happens.

I would like to display sinonimo_partita_random, but if I click it is always printed "della gara" or always "del match". While, if I close and reopen the script, the word is changed.

How can I make the random function fully functional when I click the Print button?

import random
from tkinter import ttk
import tkinter as tk
from tkinter import *

root = tk.Tk()
root.geometry("400x150")
                      
sinonimi_partita_diz = {

        "della gara": {"genere" : "femminile", "unità" : "singolare", "apostrofo" : "no"},
        "del match": {"genere" : "maschile", "unità" : "singolare", "apostrofo" : "no"},
        }

valido_a_diz = {
    
            "valido" : {
            "genere" : "maschile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },        

            "valida" : {
            "genere" : "femminile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },                
        }

####################

text = tk.Text(root,width=43,height=2)
text.place(x=15, y=50)

#THE PROBLEM IS HERE:
sinonimo_partita = ("della gara", "del match")
sinonimo_partita_random = random.choice(sinonimo_partita)

valido_a = ""
for key, value in valido_a_diz.items():
    if sinonimi_partita_diz[sinonimo_partita_random] == value:
        valido_a = key
        break

def print():
    text.delete(1.0,END)
    phrase = f"{sinonimo_partita_random} {valido_a}"
    text.insert(tk.END, phrase)

button2 = Button(root, text="Print", command = print)
button2.pack()
button2.place(x=15, y=100)

CodePudding user response:

I changed the order of your code, so the random function is now inside the print() function and at the end root.mainloop() was missing.

import random
import tkinter as tk
from tkinter import *

def print():
    sinonimo_partita_random = random.choice(sinonimo_partita)
    valido_a = ""
    for key, value in valido_a_diz.items():
        if sinonimi_partita_diz[sinonimo_partita_random] == value:
            valido_a = key
            break
    text.delete(1.0,END)
    phrase = f"{sinonimo_partita_random} {valido_a}"
    text.insert(tk.END, phrase)
                      
sinonimi_partita_diz = {

        "della gara": {"genere" : "femminile", "unità" : "singolare", "apostrofo" : "no"},
        "del match": {"genere" : "maschile", "unità" : "singolare", "apostrofo" : "no"},
        }

valido_a_diz = {
    
            "valido" : {
            "genere" : "maschile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },        

            "valida" : {
            "genere" : "femminile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },                
        }

sinonimo_partita = ["della gara", "del match"]

root = tk.Tk()
root.geometry("400x150")

text = tk.Text(root,width=43,height=2)
text.place(x=15, y=50)

button2 = Button(root, text="Print", command = print)
button2.pack()
button2.place(x=15, y=100)

root.mainloop()
  • Related