Move the dictionaries in dicts.py and create a class in template.py to return required template.You can create as many templates as you want in templates.py.
dicts.py
teams = {...}
nouns = {...}
article_words = {...}
template.py
from grammer import getArticle
import random
class Template():
def __init__(self,**kwargs):
self.__dict__.update(kwargs)
# must define other variables like article which are used
# in templates irrespective of the arguments passed
if self.subject:
self.article = getArticle(subject=self.subject)
def return_template(self):
test_template_list = ["Bla bla bla {subject} {article}", "Again bla bla bla {subject}"]
test_template = random.choice(test_template_list)
place_holders = ['{subject}','{article}', ]
for i in place_holders:
if i in test_template:
test_template=test_template.replace(i,(self.__dict__[i.lstrip("{").rstrip("}")]))
return test_template
grammer.py
from dicts import article_words, nouns
def getArticle(subject):
for key, value in article_words.items():
if nouns[subject] == value:
return key
main.py
from tkinter import *
from tkinter import ttk
from template import Template
from dicts import teams
root = Tk()
root.geometry("420x150")
tournament=ttk.Combobox(root, width = 18)
tournament.place(x=15, y=15)
tournament['value'] = ["Serie A", "Serie B"]
tournament.set("Tournament")
def on_tournament_selected(event):
team.delete(0,'end')
req_teams = []
sel_tournament = tournament.get()
for key,value in teams.items():
if value['Tournament'] == sel_tournament:
req_teams.append(key)
team.config(values=req_teams)
tournament.bind("<<ComboboxSelected>>", on_tournament_selected)
team=ttk.Combobox(root, width = 18)
team.place(x=215, y=15)
team.set("Teams")
text = Text(root,width=43,height=2)
text.place(x=15, y=50)
def printTeam():
template1 = Template(subject=team.get()) # pass keyword arguments
text.insert(END, template1.return_template())
button2 = Button(root, text="Print", command = printTeam)
button2.pack()
button2.place(x=15, y=100)
root.mainloop()
The error was due to circular imports in your files.
24
solved Print a sentence with placeholder and function from an external file. Problem with importing forms, using and retrieving content