WHAT IS THIS CODE? This little script is meant to select a tournament and automatically display the teams related to that tournament in the second combobox. The second combobox displays the teams extracted from the teams
dictionary. Easy!
Subsequently, the aim is to print the articulated preposition (feminine or masculine) relating to the chosen team, thanks to the dictionaries article_words
and nouns
. Everything works fine except the team selection which I now explain the problem.
PROBLEM: The problem is subject = "Atalanta"
, because if I manually enter the name of the team, then everything works correctly without any problem. But I want in subject there is the combobox team.get()
with which I can select any team (in this case Atalanta or Bologna) I tried to write subject = team.get()
, but I get Squadra
which is the name of the combobox set with Key Error: Squadra
(because it does not find the teams but the word of the set)
WHAT I WANT? How can I set the combobox team.get() as a subjet in order to print the article of any team I choose in the combobox? (for example by choosing Atalanta or Bologna, and not manually writing "subject =" Atalanta ").
CURRENT OUTPUT AND OUTPUT I WANT TO OBTAIN? Currently the output is dell'Atalanta
, because I insert subject = "Atalanta". I would also like to get del Bologna
output, so I need to change the subject and assign it the selectable content of the combobox team.get (). The selection of the article (del or dell' ) occurs with the loop and with the article_words dictionary.
NOTE: I don't want to write Atalanta in subject = "Atalanta"
, it was just an example. I want subject
to be the combobox team, because I need it inside the for key, value "loop in article_words.items
there is subject that is linked to the combobox team.
Here is the executable code. In the first part there is a small part of the dictionaries I use, in the second part there are the comboboxes and the function:
from tkinter import ttk
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.geometry("400x150")
teams = {
"Atalanta": {
"Nome Squadra": "Atalanta",
"Campionato": "Serie A",
"Città": "Bergamo",
},
"Bologna": {
"Nome Squadra": "Bologna",
"Campionato": "Serie A",
"Città": "Bologna",
}
}
nouns = {
"Atalanta": {"genere" : "femminile", "unità" : "singolare", "apostrofo" : "si"},
"Bologna": {"genere" : "maschile", "unità" : "singolare", "apostrofo" : "no"},
}
article_words = {
"del" : {
"genere" : "maschile",
"unità" : "singolare"
},
"dello" : {
"genere" : "maschile",
"unità" : "singolare"
},
"della" : {
"genere" : "femminile",
"unità" : "singolare"
},
"delle" : {
"genere" : "femminile",
"unità" : "plurale"
},
"dell'" : {
"genere" : "femminile",
"unità" : "singolare",
"apostrofo" : "si"
},
}
####################
tournament=ttk.Combobox(root, width = 18)
tournament.place(x=15, y=15)
tournament['value'] = ["Serie A", "Serie B"]
tournament.set("Campionato")
def on_tournament_selected(event):
# Clear the entry boxes: aggiunto io
team.delete(0,'end')
req_teams = [] # For all the required teams
sel_tournament = tournament.get() # Get the tournament
# get the names for selected gender
for _team in teams.items(): # Go through all the teams in the dictionary
key = _team[0] # Get the key
value = _team[1] # Get the value
if value['Campionato'] == sel_tournament: # If Tournament of the loop-ed team is our selected tourname, then
req_teams.append(key)
team.config(values=req_teams) # Change the values of the combobox
tournament.bind('<<ComboboxSelected>>', on_tournament_selected)
team=ttk.Combobox(root, width = 18)
team.place(x=200, y=15)
team.set("Squadra")
text = tk.Text(root,width=43,height=2)
text.place(x=15, y=50)
#THE PROBLEM IS HERE:
subject = "Atalanta"
article = ""
for key, value in article_words.items():
if nouns[subject] == value:
article = key
break
def print():
phrase = f"{article} {subject}"
text.insert(tk.END, phrase)
button2 = Button(root, text="Print", command = print)
button2.pack()
button2.place(x=15, y=100)
root.mainloop()
How can I solve? Can you show me the code I should use please? (with comments alone I might not understand why I'm starting out with Python). Thank you
CodePudding user response:
To get the value from a combobox you need to attach a variable to it.
First initialize the variable teamname = Tk.StringVar(value='')
, here I initialized it with an empty string.
Then attach this variable to the combobox, team.configure(textvariable=teamname)
.
Now when the combobox box value changes, the teamname
variable will also change.
Now to get the value use teamname.get()
.
So in the above example this would look like ...
from tkinter import ttk
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.geometry("400x150")
teams = {
"Atalanta": {
"Nome Squadra": "Atalanta",
"Campionato": "Serie A",
"Città": "Bergamo",
},
"Bologna": {
"Nome Squadra": "Bologna",
"Campionato": "Serie A",
"Città": "Bologna",
}
}
nouns = {
"Atalanta": {"genere" : "femminile", "unità" : "singolare", "apostrofo" : "si"},
"Bologna": {"genere" : "maschile", "unità" : "singolare", "apostrofo" : "no"},
}
article_words = {
"del" : {
"genere" : "maschile",
"unità" : "singolare",
"apostrofo": "no"
},
"dello" : {
"genere" : "maschile",
"unità" : "singolare"
},
"della" : {
"genere" : "femminile",
"unità" : "singolare"
},
"delle" : {
"genere" : "femminile",
"unità" : "plurale"
},
"dell'" : {
"genere" : "femminile",
"unità" : "singolare",
"apostrofo" : "si"
},
}
####################
tournament=ttk.Combobox(root, width = 18)
tournament.place(x=15, y=15)
tournament['value'] = ["Serie A", "Serie B"]
tournament.set("Campionato")
def on_tournament_selected(event):
# Clear the entry boxes: aggiunto io
team.delete(0,'end')
req_teams = [] # For all the required teams
sel_tournament = tournament.get() # Get the tournament
# get the names for selected gender
for _team in teams.items(): # Go through all the teams in the dictionary
key = _team[0] # Get the key
value = _team[1] # Get the value
if value['Campionato'] == sel_tournament: # If Tournament of the loop-ed team is our selected tourname, then
req_teams.append(key)
team.config(values=req_teams) # Change the values of the combobox
tournament.bind('<<ComboboxSelected>>', on_tournament_selected)
team=ttk.Combobox(root, width = 18)
team.place(x=200, y=15)
team.set("Squadra")
text = tk.Text(root,width=43,height=2)
text.place(x=15, y=50)
def printTeam():
subject = team.get()
for key, value in article_words.items():
if nouns[subject] == value:
article = key
phrase = f"{article} {subject}"
text.insert(tk.END, phrase)
button2 = Button(root, text="Print", command = printTeam)
button2.pack()
button2.place(x=15, y=100)
root.mainloop()