Im learing python tkinter by making simple program where you connect english words with their spanish counterparts. Code contains two files, frist has title and a button that if clicked turn an actuall code where you connect words.
The command in button in the frist file is imported from the second file, in this second file to set up a new screen im importing class called screen and three others tkinter widgets, although that part is unfishied and is working poorly.
The point is i cant continue my learning because after frist lanuch where everything is working as i intented it to work, when i click run button this second time in vsc nothing happens excepct that python procces will occur in the background. If i click run button three times there will be 3 procces of it although code still wont run. Only after i close these procces every run will pop out on screen as it should.
I expect that after frist run is closed some procces in the second file are still running and thats why it transfer to the background procces although i dont know how can i fix that even if thats it.
Here is the frist ‘menu’ file code:
import tkinter as tk
class screen():
def __init__(self, back_or_not = 0, create_new_screen = 0):
super().__init__()
global unknown_screen, frist_label
# if __name__ == '__main__' or create_new_screen == 1:
if back_or_not == 0:
self.root = tk.Tk()
self.root.title('LearnIt!')
self.root.geometry('1920x1080')
self.root.configure(bg='black')
class clil():
def __init__(self):
super().__init__()
if __name__ == 'menu_learnit':
frist_screen.root.withdraw()
self.lil_f = ('Kozuka Mincho Pro M', 50, 'bold')
self.basic_f = ('Lionel Classic', 50)
self.Learn_it_label = tk.Label(frist_screen.root, text='LearnIt!', bg='black', fg='yellow', height=2, width=10)
self.Learn_it_label.configure(font=self.lil_f)
self.Learn_it_label.place(y=0, x=725)
class csb():
def __init__(self):
super().__init__()
if __name__ == '__main__':
from mat_but_learnit import spanish_button_func
self.spanish_button = tk.Button(frist_screen.root, text='Spanish connect!', bg='black', fg='orange', height=1, width=len('Spanish connect'), command= lambda: spanish_button_func('pronto', 'soon', 'frio', 'cold', 'calor', 'hot', 'silla', 'chair', 'cuchillo', 'knife'))
self.spanish_button.configure(font=frist_label.basic_f)
self.spanish_button.place(y=160, x=1400)
if __name__ == '__main__':
frist_screen.root.mainloop()
frist_screen = screen()
print('1')
frist_label = clil()
print('2')
frist_button = csb()
print('3')
Here is the second file with the actual code it has almost 400 lines so i put here the part i think is important, the rest of the code is at the end of question. (This part maybe dont make sense i dont know i was just learning it.)
# SETTING UP SCREEN # from menu_learnit import screen, frist_label, frist_button, frist_screen frist_label.Learn_it_label.destroy() frist_button.spanish_button.destroy() frist_screen.root.withdraw() second_screen = screen() words_frame = tk.Frame(second_screen.root, bg = 'black') for i in range(5): words_frame.columnconfigure( i, weight =1, uniform = 'a') for i in range(5): words_frame.rowconfigure(i, weight = 1, uniform = 'a')
I tried do something with imports thinking its some strange circular error but i guees it wasnt that, it would be detetected by python if it was anyway.
Reseting vsc or computer didnt work im pretty sure something is wrong with this second file because code is being transfered to the background procces only after i click the button in the frist file so only when the second file code has been runned problem occurs.
Here is the rest of the code although is kinda long for a single question so im giving it here just in case. And thanks in advice for any help.
import tkinter as tk
import random
global clicked_buttons_count_eng, clicked_buttons_count_esp, previous_button, previous_eng_word, previous_esp_word, stay_green_esp, stay_green_eng
clicked_buttons_count_eng: int = 0
clicked_buttons_count_esp: int = 0
previous_button_esp = None
previous_button_eng = None
previous_eng_word = None
previous_esp_word = None
stay_green_eng=[]
stay_green_esp=[]
def nothing():
pass
def clear():
global previous_eng_word, previous_button_eng, previous_button, previous_button_esp, previous_esp_word, button_check_spanish, button_check_english
previous_eng_word = None
previous_button_eng = None
previous_button= None
previous_word = None
previous_button_esp = None
previous_esp_word = None
button_check_spanish = None
button_check_english = None
# BUTTONS CODE
def spanish_button_func(spanish_word0_world, english_word0_world, spanish_word1_world, english_word1_world, spanish_word2_world, english_word2_world, spanish_word3_world, english_word3_world, spanish_word4_world, english_word4_world):
if __name__ == "mat_but_learnit":
# UNCORRECT MATCHES
def uncorrect():
# COLOR RED EFFECT
def color_red(whatever_button, whatever_word, func):
whatever_button.after(1000, lambda: whatever_button.config(fg='orange',command=lambda: func(whatever_button, whatever_word)))
######
global clicked_buttons_count_eng, clicked_buttons_count_esp, previous_button, unknown_button_f,no_yellow, unknown_word_f, stay_green_eng, previous_word, button_check_spanish, button_check_english
no_yellow = True
unknown_button_f.config(fg='red', command = nothing)
previous_button.config(fg='red', command = nothing)
if unknown_button_f in spanish_list:
color_red(unknown_button_f, unknown_word_f, button_connect_spanish)
color_red(previous_button, previous_word, button_connect_english)
else:
print(f'unknown_button_f { unknown_button_f} and previous_button { previous_button}.')
color_red(unknown_button_f, unknown_word_f, button_connect_english)
color_red(previous_button, previous_word, button_connect_spanish)
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
clear()
# CORRECT MATCHES
def correct(f_unknown_word, sg_number, idiom):
global clicked_buttons_count_eng,no_yellow, clicked_buttons_count_esp, stay_green_eng, stay_green_esp
if unknown_word_f == f_unknown_word:
print('xd')
no_yellow = True
if sg_number == 0:
spanish_word0.config(fg='green', command=nothing)
english_word0.config(fg='green', command=nothing)
if sg_number == 1:
spanish_word1.config(fg='green', command=nothing)
english_word1.config(fg='green', command=nothing)
if sg_number == 2:
spanish_word2.config(fg='green', command=nothing)
english_word2.config(fg='green', command=nothing)
if sg_number == 3:
spanish_word3.config(fg='green', command=nothing)
english_word3.config(fg='green', command=nothing)
if sg_number == 4:
spanish_word4.config(fg='green', command=nothing)
english_word4.config(fg='green', command=nothing)
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
if idiom == 'eng':
print('stay green' + str(sg_number))
stay_green_eng.append('stay green' + str(sg_number))
if idiom == 'esp':
stay_green_esp.append('stay green' + str(sg_number))
clear()
# COLORING KEEPING ONLY ONE BUTTON YELLOW ENG
def one_orange_english(f_previous_eng_word):
global stay_green_eng, clicked_buttons_count_eng
if f_previous_eng_word == english_word0_world:
print('one_orange_english(line96)')
if not 'stay green0' in stay_green_eng:
english_word0.configure(fg='orange', command=lambda: button_connect_english(english_word0, english_word0_world))
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
if f_previous_eng_word == english_word1_world:
print('line(105)')
if not 'stay green1' in stay_green_eng:
print('line(107)')
english_word1.configure(fg='orange', command=lambda: button_connect_english(english_word1, english_word1_world))
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
if f_previous_eng_word == english_word2_world:
if not 'stay green2' in stay_green_eng:
english_word2.configure(fg='orange', command=lambda: button_connect_english(english_word2, english_word2_world))
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
if f_previous_eng_word == english_word3_world:
if not 'stay green3' in stay_green_eng:
english_word3.configure(fg='orange', command=lambda: button_connect_english(english_word3, english_word3_world))
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
if f_previous_eng_word == english_word4_world:
if not 'stay green4' in stay_green_eng:
english_word4.configure(fg='orange', command=lambda: button_connect_english(english_word4, english_word4_world))
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
# COLORING KEEPING ONLY ONE BUTTON YELLOW ESP
def one_orange_esp(f_previous_esp_word):
global stay_green_esp, clicked_buttons_count_esp
if f_previous_esp_word == spanish_word0_world:
if not 'stay green0' in stay_green_esp:
spanish_word0.configure(fg='orange', command=lambda: button_connect_spanish(spanish_word0, spanish_word0_world))
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
if f_previous_esp_word == spanish_word1_world:
if not 'stay green1' in stay_green_esp:
spanish_word1.configure(fg='orange', command=lambda: button_connect_spanish(spanish_word1, spanish_word1_world))
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
if f_previous_esp_word == spanish_word2_world:
if not 'stay green2' in stay_green_esp:
spanish_word2.configure(fg='orange', command=lambda: button_connect_spanish(spanish_word2, spanish_word2_world))
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
if f_previous_esp_word == spanish_word3_world:
if not 'stay green3' in stay_green_esp:
spanish_word3.configure(fg='orange', command=lambda: button_connect_spanish(spanish_word3, spanish_word3_world))
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
if f_previous_esp_word == spanish_word4_world:
if not 'stay green4' in stay_green_esp:
spanish_word4.configure(fg='orange', command=lambda: button_connect_spanish(spanish_word4, spanish_word4_world))
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
# ENGLISH BUTTONS
def button_connect_english(unknown_button, unknown_word):
global clicked_buttons_count_eng, previous_eng_word, previous_button_eng, stay_green_eng, previous_button, button_check_english, previous_esp_word, unknown_word_f, clicked_buttons_count_esp, previous_word, no_yellow, unknown_button_f
correct_value = 0
no_yellow = False
button_check_english = str(unknown_word)
unknown_button_f = unknown_button
unknown_word_f = unknown_word
# COUNTING BUTTONS
clicked_buttons_count_eng = clicked_buttons_count_eng + 1
clicked_buttons_count = clicked_buttons_count_eng + clicked_buttons_count_esp
# BUTTONS INTERACTIONS
if clicked_buttons_count > 1:
print('clicked_buttons_count(line:179)')
if clicked_buttons_count_eng > 1:
print('one orange english(line:181)')
one_orange_english(str(previous_eng_word))
if clicked_buttons_count_esp == 1:
try:
print('clicked_buttons_count(line:185)')
if button_conections_dict[button_check_english] == button_check_spanish or button_conections_dict[button_check_spanish] == button_check_english:
print('clicked_buttons_count(line:187)')
correct(english_word0_world, 0, 'eng')
correct(english_word1_world, 1, 'eng')
correct(english_word2_world, 2,'eng')
correct(english_word3_world, 3,'eng')
correct(english_word4_world, 4,'eng')
if clicked_buttons_count_eng == 1:
if not button_check_spanish == None:
if not button_conections_dict[button_check_english] == button_check_spanish or not button_conections_dict[button_check_spanish] == button_check_english:
uncorrect()
except NameError:
pass
except KeyError:
pass
# CHANGING DATA FOR NEW AFTER INTERACTIONS
previous_eng_word = str(unknown_word)
previous_button_eng = str(unknown_button)
previous_button = unknown_button
previous_word = unknown_word
# DOUBLE CLICK
def button_connect22(unknown_button2):
global clicked_buttons_count_esp, clicked_buttons_count_eng, previous_button
previous_button = None
previous_word = None
if not clicked_buttons_count_eng == 0:
clicked_buttons_count_eng = clicked_buttons_count_eng - 1
unknown_button2.configure(bg='black', fg='orange', command=lambda:button_connect_english(unknown_button2, unknown_word))
# ONE CLICK
if no_yellow == False:
unknown_button.configure(bg='black', fg='yellow', command=lambda:button_connect22(unknown_button))
# SPANISH BUTTTONS
def button_connect_spanish(unknown_button, unknown_word):
global clicked_buttons_count_esp, clicked_buttons_count_esp, previous_esp_word, previous_button_esp, stay_green_esp, previous_button, button_check_spanish, previous_word, clicked_buttons_count_eng, unknown_button_f, no_yellow, unknown_word_f
correct_value = 0
no_yellow = False
unknown_button_f = unknown_button
unknown_word_f = unknown_word
button_check_spanish = str(unknown_word)
# COUNTING BUTTONS
clicked_buttons_count_esp = clicked_buttons_count_esp + 1
clicked_buttons_count = clicked_buttons_count_eng + clicked_buttons_count_esp
# BUTTONS INTERACTIONS
if clicked_buttons_count > 1:
if clicked_buttons_count_esp > 1:
print('ssak')
one_orange_esp(previous_esp_word)
if clicked_buttons_count_esp == 1:
try:
if button_conections_dict[button_check_english] == button_check_spanish or button_conections_dict[button_check_spanish] == button_check_english:
correct(spanish_word0_world, 0, 'esp')
correct(spanish_word1_world, 1, 'esp')
correct(spanish_word2_world, 2, 'esp')
correct(spanish_word3_world, 3, 'esp')
correct(spanish_word4_world, 4, 'esp')
if clicked_buttons_count_esp == 1:
if not button_check_english == None:
if not button_conections_dict[button_check_english] == button_check_spanish or not button_conections_dict[button_check_spanish] == button_check_english:
uncorrect()
except NameError:
pass
except KeyError:
pass
# CHANGING DATA FOR NEW AFTER INTERACTIONS
previous_esp_word = str(unknown_word)
previous_button_esp = str(unknown_button)
previous_word = unknown_word
previous_button = unknown_button
# DOUBLE CLICK
def button_connect2(unknown_button2):
global clicked_buttons_count_esp, clicked_buttons_count_eng, previous_button
previous_button = None
previous_word = None
if not clicked_buttons_count_esp == 0:
clicked_buttons_count_esp = clicked_buttons_count_esp - 1
unknown_button2.configure(bg='black', fg='orange', command=lambda:button_connect_spanish(unknown_button2, unknown_word))
# ONE CLICK
if no_yellow == False:
unknown_button.configure( fg = 'yellow', command=lambda:button_connect2(unknown_button))
# SETTING UP SCREEN ###################################################################################################################################################################################################
print('JEBANE IMPORTY22222222222222222222222222222222222')
from menu_learnit import screen, frist_label, frist_button, frist_screen
# from menu_learnit import frist_label
print('JEBANE IMPORTY')
frist_label.Learn_it_label.destroy()
frist_button.spanish_button.destroy()
frist_screen.root.withdraw()
second_screen = screen()
words_frame = tk.Frame(second_screen.root, bg = 'black')
for i in range(5):
words_frame.columnconfigure( i, weight =1, uniform = 'a')
for i in range(5):
words_frame.rowconfigure(i, weight = 1, uniform = 'a')
# RANDOM ORDER OF WORDS##################################################################################################################################################################################
rng_rows = [0,1,2,3,4]
random_row_esp0 = random.choice(rng_rows)
rng_rows.remove(random_row_esp0)
random_row_esp1 = random.choice(rng_rows)
rng_rows.remove(random_row_esp1)
random_row_esp2 = random.choice(rng_rows)
rng_rows.remove(random_row_esp2)
random_row_esp3 = random.choice(rng_rows)
rng_rows.remove(random_row_esp3)
random_row_esp4 = random.choice(rng_rows)
rng_rows.remove(random_row_esp4)
rng_rows = [0,1,2,3,4]
random_row_eng0 = random.choice(rng_rows)
rng_rows.remove(random_row_eng0)
random_row_eng1 = random.choice(rng_rows)
rng_rows.remove(random_row_eng1)
random_row_eng2 = random.choice(rng_rows)
rng_rows.remove(random_row_eng2)
random_row_eng3 = random.choice(rng_rows)
rng_rows.remove(random_row_eng3)
random_row_eng4 = random.choice(rng_rows)
rng_rows.remove(random_row_eng4)
###################################################################################################################################################################################################
# WORD PLACEMENT ##################################################################################################################################################################################
spanish_word0 = tk.Button(words_frame, text=spanish_word0_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f, command=lambda:button_connect_spanish(spanish_word0, spanish_word0_world))
spanish_word0.grid(column=1, row=random_row_esp0, padx = 30, pady= 30)
spanish_word1 = tk.Button(words_frame, text=spanish_word1_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f, command=lambda:button_connect_spanish(spanish_word1, spanish_word1_world))
spanish_word1.grid(column=1, row=random_row_esp1, padx = 30, pady= 30)
spanish_word2 = tk.Button(words_frame, text=spanish_word2_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f, command=lambda:button_connect_spanish(spanish_word2, spanish_word2_world))
spanish_word2.grid(column=1, row=random_row_esp2, padx = 30, pady= 30)
spanish_word3 = tk.Button(words_frame, text=spanish_word3_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f, command=lambda:button_connect_spanish(spanish_word3, spanish_word3_world))
spanish_word3.grid(column=1, row=random_row_esp3, padx = 30, pady= 30)
spanish_word4 = tk.Button(words_frame, text=spanish_word4_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f, command=lambda:button_connect_spanish(spanish_word4, spanish_word4_world))
spanish_word4.grid(column=1, row=random_row_esp4, padx = 30, pady= 30)
english_word0 = tk.Button(words_frame, text=english_word0_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f,command=lambda:button_connect_english(english_word0, english_word0_world))
english_word0.grid(column=3, row=random_row_eng0)
english_word1 = tk.Button(words_frame, text=english_word1_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f,command=lambda:button_connect_english(english_word1, english_word1_world))
english_word1.grid(column=3, row=random_row_eng1)
english_word2 = tk.Button(words_frame, text=english_word2_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f,command=lambda:button_connect_english(english_word2, english_word2_world))
english_word2.grid(column=3, row=random_row_eng2)
english_word3 = tk.Button(words_frame, text=english_word3_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f,command=lambda:button_connect_english(english_word3, english_word3_world))
english_word3.grid(column=3, row=random_row_eng3)
english_word4 = tk.Button(words_frame, text=english_word4_world, bg='black', fg='orange', height=1, width=10, font=frist_label.basic_f, command=lambda:button_connect_english(english_word4, english_word4_world))
english_word4.grid(column=3, row=random_row_eng4)
###################################################################################################################################################################################################
# LISTS AND DICTS##################################################################################################################################################################################
spanish_list=[spanish_word0, spanish_word1, spanish_word2, spanish_word3, spanish_word4]
english_list =[english_word0, english_word1, english_word2, english_word3, english_word4]
button_conections_dict = {spanish_word0_world : english_word0_world, english_word0_world : spanish_word0_world, spanish_word1_world : english_word1_world, english_word1_world : spanish_word1_world, spanish_word2_world : english_word2_world, english_word2_world : spanish_word2_world, spanish_word3_world : english_word3_world, english_word3_world : spanish_word3_world, spanish_word4_world : english_word4_world, english_word4_world : spanish_word4_world}
###################################################################################################################################################################################################
# CONTINUE BUTTON
def con_but_fun():
global clicked_buttons_count_eng, clicked_buttons_count_esp, clicked_buttons_count
clear()
clicked_buttons_count_eng = 0
clicked_buttons_count_esp = 0
clicked_buttons_count = 0
stay_green_esp.clear()
stay_green_eng.clear()
words_frame.forget()
# import menu_learnit
# Learn_it_main(1)
continue_button = tk.Button(words_frame, text='continue', bg='black', fg='yellow', font=frist_label.basic_f, command = lambda: con_but_fun())
continue_button.grid(column=4, row=4, padx = 30, pady= 30)
words_frame.pack()
words_frame.mainloop()