Почему после первого нажатия кнопки результат не обновляется?
главный код:
from tkinter import *
from tkinter.ttk import Notebook, Progressbar
from time import *
from Settings import Settings
from tkinter import messagebox as mb
class App:
def __init__(self):
self.theme_button = None
self.root = None
self.tk = None
self.tabs_control = None
self.tab1 = None
self.tab2 = None
self.tab3 = None
self.tab4 = None
self.pb = None
self.p = None
self.sb = None
self.salary_lbl_frame = None
self.salary_input = None
self.month = 0
self.day = self.month // 30
self.year = self.month * 12
self.week = self.day * 7
self.send_res = None
self.year_res = Label()
self.month_res = Label()
self.week_res = Label()
self.day_res = Label()
self.res1 = (f""
f"monthly salary is\t{self.month}")
self.res2 = f"yearly salary is\t{self.year}"
self.res3 = f"weekly salary is\t{self.week}"
self.res4 = f"daily salary is\t{self.day}"
self.results = None
self.res_control = True
self.label = True
self.set = Settings()
self.check_theem_lbl = None
self.error = None
self.iserror = False
self.theem = 'light'
def change(self):
if self.theem == 'light':
self.theem = 'dark'
self.tab1['bg'] = self.set.thems[self.theem]["tab_color"]
self.tab2['bg'] = self.set.thems[self.theem]["tab_color"]
self.tab3['bg'] = self.set.thems[self.theem]["tab_color"]
self.tab4['bg'] = self.set.thems[self.theem]["tab_color"]
self.salary_lbl_frame['bg'] = self.set.thems[self.theem]['lbl-frame_color']
self.salary_input['bg'] = self.set.thems[self.theem]["etr_color"]
self.year_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.month_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.week_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.day_res['bg'] = self.set.thems[self.theem]["tab_color"]
else:
self.theem = 'light'
self.tab1['bg'] = self.set.thems[self.theem]["tab_color"]
self.tab2['bg'] = self.set.thems[self.theem]["tab_color"]
self.tab3['bg'] = self.set.thems[self.theem]["tab_color"]
self.tab4['bg'] = self.set.thems[self.theem]["tab_color"]
self.salary_lbl_frame['bg'] = self.set.thems[self.theem]['lbl-frame_color']
self.salary_input['bg'] = self.set.thems[self.theem]["etr_color"]
self.year_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.month_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.week_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.day_res['bg'] = self.set.thems[self.theem]["tab_color"]
def notebook_code(self):
# setup Notebook
self.tabs_control = Notebook(self.tk, padding=(20,))
self.tabs_control.enable_traversal()
self.tab1 = Frame(self.tabs_control, bg=self.set.thems[self.theem]["tab_color"])
self.tab2 = Frame(self.tabs_control, bg=self.set.thems[self.theem]["tab_color"])
self.tab3 = Frame(self.tabs_control, bg=self.set.thems[self.theem]["tab_color"])
self.tab4 = Frame(self.tabs_control, bg=self.set.thems[self.theem]["tab_color"])
self.tabs_control.add(self.tab1, text='Input', underline=0)
self.tabs_control.add(self.tab2, text='Output', underline=0)
self.tabs_control.add(self.tab3, text="Settings", underline=0)
self.tabs_control.add(self.tab4, text="About me", underline=0)
self.tabs_control.select(self.tab1)
def widgets(self):
# title
title = Label(
self.tk,
text=' salary calculator ',
bg='black',
fg='white',
font='Arial 20 bold')
title.place(rely=0.0399, relx=0.5, anchor=CENTER)
# Notebook widgets
self.tabs_control.pack(pady=20, fill=BOTH, expand=True)
# first tab
self.salary_lbl_frame = LabelFrame(self.tab1, text='write here your monthly salary',
padx=20,
pady=20,
font="Arial 12 bold",
highlightthickness=0,
bg=self.set.thems[self.theem]['lbl-frame_color'])
self.salary_input = Entry(self.salary_lbl_frame, bg=self.set.thems[self.theem]["etr_color"])
self.send_res = Button(
self.salary_lbl_frame,
text="send",
command=self.send_results,
padx=45,
bg=self.set.thems[self.theem]['send-btn_color'],
)
self.salary_lbl_frame.pack(pady=30)
self.salary_input.pack()
self.send_res.pack()
# second tab
self.results = Label(
self.tab2,
text=' results '
' ',
bg='black', fg='white')
# third tab
self.theme_button = Button(self.tab3, text='swich theem', command=self.change)
self.theme_button.pack()
exit_btn = Button(
self.tab3,
text='Exit',
bg='maroon',
fg='black',
font='Arial 8 bold',
command=self.exit,
pady=5,
padx=1000
)
exit_btn.place(rely=0.96, relx=0.5, anchor=CENTER)
def send_results(self):
try:
self.salary_input.update()
self.month = float(self.salary_input.get())
self.year = self.month * 12
self.day = self.month // 30
self.week = self.day * 7
self.res1 = f"\r\nmonthly salary is\t{self.month}"
self.res2 = f" yearly salary is\t{self.year}"
self.res3 = f"weekly salary is\t{self.week}"
self.res4 = f"daily salary is\t{self.day}"
self.year_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.month_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.week_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.day_res['bg'] = self.set.thems[self.theem]["tab_color"]
self.month_res = Label(self.tab2, text=self.res1, font='Arial 14 bold', bg=self.set.thems[self.theem]['output_color'])
self.year_res = Label(self.tab2, text=self.res2, font='Arial 14 bold', bg=self.set.thems[self.theem]['output_color'])
self.week_res = Label(self.tab2, text=self.res3, font='Arial 14 bold', bg=self.set.thems[self.theem]['output_color'])
self.day_res = Label(self.tab2, text=self.res4, font='Arial 14 bold', bg=self.set.thems[self.theem]["output_color"])
self.results.pack()
if self.label:
self.month_res.pack()
self.year_res.pack()
self.week_res.pack()
self.day_res.pack()
self.label = False
else:
print('alright')
self.month_res['text'] = self.res1
self.year_res['text'] = self.res2
self.week_res['text'] = self.res3
self.day_res['text'] = self.res4
except ValueError:
mb.showerror(title='ValueError', message='ValueError')
def open(self):
self.root = Tk()
self.root.geometry("+820+400")
Label(self.root, text='Loading', font="Arial 15 bold").pack()
self.month_res.pack()
self.year_res.pack()
self.week_res.pack()
self.day_res.pack()
self.pb = Progressbar(self.root, orient=HORIZONTAL, mode="determinate", length=400)
self.pb.pack(pady=20)
for i in range(101):
self.pb.configure(value=i)
self.pb.update()
sleep(0.005)
self.root.destroy()
@staticmethod
def exit():
mb.askokcancel("exit", 'exit')
exit()
def run(self):
self.open()
self.tk = Tk()
self.tk.title('Salary calculator 1.6')
self.tk.geometry('500x500+720+225')
self.tk.resizable(False, False)
self.notebook_code()
self.widgets()
while self.tk:
self.tk.update()
self.tk.mainloop()
self.exit()
app = App()
app.run()
настройки:
class Settings:
def __init__(self):
self.daily = True
self.weekly = True
self.monthly = True
self.yearly = True
self.thems = {
'dark': {
'tab_color': 'grey50',
'send-btn_color': 'grey70',
'etr_color': 'grey50',
'lbl-frame_color': 'grey60',
'output_color': "grey50"
},
'light': {
'tab_color': 'grey90',
'send-btn_color': 'grey80',
'etr_color': 'grey90',
'lbl-frame_color': 'grey80',
'output_color': "grey90"
}
}
Ответы (1 шт):
Автор решения: iglebov
→ Ссылка
Попробуйте запустить данный код:
# главный код:
from tkinter import *
from tkinter.ttk import Notebook, Progressbar
from time import *
from tkinter import messagebox as mb
class Settings:
def __init__(self):
self.daily = True
self.weekly = True
self.monthly = True
self.yearly = True
self.themes = {
'dark': {
'tab_color': 'grey50',
'send-btn_color': 'grey70',
'etr_color': 'grey50',
'lbl-frame_color': 'grey60',
'output_color': "grey50"
},
'light': {
'tab_color': 'grey90',
'send-btn_color': 'grey80',
'etr_color': 'grey90',
'lbl-frame_color': 'grey80',
'output_color': "grey90"
}
}
class App:
def __init__(self):
self.theme_button = None
self.root = None
self.tk = None
self.tabs_control = None
self.tab1 = None
self.tab2 = None
self.tab3 = None
self.tab4 = None
self.pb = None
self.p = None
self.sb = None
self.salary_lbl_frame = None
self.salary_input = None
self.month = 0
self.day = self.month // 30
self.year = self.month * 12
self.week = self.day * 7
self.send_res = None
self.year_res = Label()
self.month_res = Label()
self.week_res = Label()
self.day_res = Label()
self.res1 = (f""
f"monthly salary is\t{self.month}")
self.res2 = f"yearly salary is\t{self.year}"
self.res3 = f"weekly salary is\t{self.week}"
self.res4 = f"daily salary is\t{self.day}"
self.results = None
self.res_control = True
self.label = True
self.set = Settings()
self.check_theme_lbl = None
self.error = None
self.iserror = False
self.theme = 'light'
def change(self):
if self.theme == 'light':
self.theme = 'dark'
self.tab1['bg'] = self.set.themes[self.theme]["tab_color"]
self.tab2['bg'] = self.set.themes[self.theme]["tab_color"]
self.tab3['bg'] = self.set.themes[self.theme]["tab_color"]
self.tab4['bg'] = self.set.themes[self.theme]["tab_color"]
self.salary_lbl_frame['bg'] = self.set.themes[self.theme]['lbl-frame_color']
self.salary_input['bg'] = self.set.themes[self.theme]["etr_color"]
self.year_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.month_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.week_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.day_res['bg'] = self.set.themes[self.theme]["tab_color"]
else:
self.theme = 'light'
self.tab1['bg'] = self.set.themes[self.theme]["tab_color"]
self.tab2['bg'] = self.set.themes[self.theme]["tab_color"]
self.tab3['bg'] = self.set.themes[self.theme]["tab_color"]
self.tab4['bg'] = self.set.themes[self.theme]["tab_color"]
self.salary_lbl_frame['bg'] = self.set.themes[self.theme]['lbl-frame_color']
self.salary_input['bg'] = self.set.themes[self.theme]["etr_color"]
self.year_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.month_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.week_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.day_res['bg'] = self.set.themes[self.theme]["tab_color"]
def notebook_code(self):
# setup Notebook
self.tabs_control = Notebook(self.tk, padding=(20,))
self.tabs_control.enable_traversal()
self.tab1 = Frame(self.tabs_control, bg=self.set.themes[self.theme]["tab_color"])
self.tab2 = Frame(self.tabs_control, bg=self.set.themes[self.theme]["tab_color"])
self.tab3 = Frame(self.tabs_control, bg=self.set.themes[self.theme]["tab_color"])
self.tab4 = Frame(self.tabs_control, bg=self.set.themes[self.theme]["tab_color"])
self.tabs_control.add(self.tab1, text='Input', underline=0)
self.tabs_control.add(self.tab2, text='Output', underline=0)
self.tabs_control.add(self.tab3, text="Settings", underline=0)
self.tabs_control.add(self.tab4, text="About me", underline=0)
self.tabs_control.select(self.tab1)
def widgets(self):
# title
title = Label(
self.tk,
text=' salary calculator ',
bg='black',
fg='white',
font='Arial 20 bold')
title.place(rely=0.0399, relx=0.5, anchor=CENTER)
# Notebook widgets
self.tabs_control.pack(pady=20, fill=BOTH, expand=True)
# first tab
self.salary_lbl_frame = LabelFrame(self.tab1, text='write here your monthly salary',
padx=20,
pady=20,
font="Arial 12 bold",
highlightthickness=0,
bg=self.set.themes[self.theme]['lbl-frame_color'])
self.salary_input = Entry(self.salary_lbl_frame, bg=self.set.themes[self.theme]["etr_color"])
self.send_res = Button(
self.salary_lbl_frame,
text="send",
command=self.send_results,
padx=45,
bg=self.set.themes[self.theme]['send-btn_color'],
)
self.salary_lbl_frame.pack(pady=30)
self.salary_input.pack()
self.send_res.pack()
# second tab
self.results = Label(
self.tab2,
text=' results '
' ',
bg='black', fg='white')
# third tab
self.theme_button = Button(self.tab3, text='swich theme', command=self.change)
self.theme_button.pack()
exit_btn = Button(
self.tab3,
text='Exit',
bg='maroon',
fg='black',
font='Arial 8 bold',
command=self.exit,
pady=5,
padx=1000
)
exit_btn.place(rely=0.96, relx=0.5, anchor=CENTER)
def send_results(self):
try:
self.salary_input.update()
self.month = float(self.salary_input.get())
self.year = self.month * 12
self.day = self.month // 30
self.week = self.day * 7
self.res1 = f"\r\nmonthly salary is\t{self.month}"
self.res2 = f" yearly salary is\t{self.year}"
self.res3 = f"weekly salary is\t{self.week}"
self.res4 = f"daily salary is\t{self.day}"
self.results.pack()
if self.label:
self.year_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.month_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.week_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.day_res['bg'] = self.set.themes[self.theme]["tab_color"]
self.month_res = Label(self.tab2, text=self.res1, font='Arial 14 bold',
bg=self.set.themes[self.theme]['output_color'])
self.year_res = Label(self.tab2, text=self.res2, font='Arial 14 bold',
bg=self.set.themes[self.theme]['output_color'])
self.week_res = Label(self.tab2, text=self.res3, font='Arial 14 bold',
bg=self.set.themes[self.theme]['output_color'])
self.day_res = Label(self.tab2, text=self.res4, font='Arial 14 bold',
bg=self.set.themes[self.theme]["output_color"])
self.month_res.pack()
self.year_res.pack()
self.week_res.pack()
self.day_res.pack()
self.label = False
else:
print('alright')
self.month_res['text'] = self.res1
self.year_res['text'] = self.res2
self.week_res['text'] = self.res3
self.day_res['text'] = self.res4
self.root.update()
except ValueError:
mb.showerror(title='ValueError', message='ValueError')
def open(self):
self.root = Tk()
self.root.geometry("+820+400")
Label(self.root, text='Loading', font="Arial 15 bold").pack()
self.month_res.pack()
self.year_res.pack()
self.week_res.pack()
self.day_res.pack()
self.pb = Progressbar(self.root, orient=HORIZONTAL, mode="determinate", length=400)
self.pb.pack(pady=20)
for i in range(101):
self.pb.configure(value=i)
self.pb.update()
sleep(0.005)
self.root.destroy()
@staticmethod
def exit():
mb.askokcancel("exit", 'exit')
exit()
def run(self):
self.open()
self.tk = Tk()
self.tk.title('Salary calculator 1.6')
self.tk.geometry('500x500+720+225')
self.tk.resizable(False, False)
self.notebook_code()
self.widgets()
while self.tk:
self.tk.update()
self.tk.mainloop()
self.exit()
app = App()
app.run()