Hey I would like to create a frame which contains a Listbox and some buttons. What I would like to do is to somehow pass as an argument a name of this listbox. This is my current code in which I set the name of this listbox to master.thisListbox but I would like to pass it somehow as argument. Is it possible or should I separately crate a listbox in my App and the pass it as an argument to a frame? I'm going to create a lot of such listboxes in my App hence the name of listbox shouldn't be the same. I'm pretty new in OOP.
class myApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.dateFrame = DateFrame(self)
self.dateFrame.grid(row = 0, column = 0)
print(self.day.get())
self.myFrame = ListboxFrame(self, "Label!", [1,2,3])
self.myFrame.grid(row = 1, column = 0)
x = self.thisListbox.get(0, END)
print(x)
class DateFrame(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
# Day
ttk.Label(self, text = "Day", width = 4).grid(row = 0, column = 0, padx = 3, pady = 3)
master.day = IntVar(master, value = 31)
self.dayCB = ttk.Combobox(self, values = [x for x in range(1, 32)], textvariable = master.day, width = 4)
self.dayCB.grid(row = 1, column = 0, padx = 3, pady = 3)
# Month
ttk.Label(self, text = "Month", width = 6).grid(row = 0, column = 1, padx = 3, pady = 3)
self.month = IntVar(master, value = 12)
self.monthCB = ttk.Combobox(self, values = [x for x in range(1, 13)], textvariable = self.month, width = 4)
self.monthCB.grid(row = 1, column = 1, padx = 3, pady = 3)
# Year
ttk.Label(self, text = "Year", width = 4).grid(row = 0, column = 2, padx = 3, pady = 3)
self.year = IntVar(master, value = 2021)
self.yearCB = ttk.Spinbox(self, from_ = 2020, to = 2100, textvariable = self.year, width = 6)
self.yearCB.grid(row = 1, column = 2, padx = 3, pady = 3)
class ListboxFrame(ttk.Frame):
def __init__(self, master, labelText, values, selectmode = "extended", height = 6, width = 30):
ttk.Frame.__init__(self, master)
# Listbox
ttk.Label(self, text = labelText).grid(row = 0, column = 0, columnspan = 3)
master.thisListbox = tk.Listbox(self, selectmode = selectmode, height = height, width = width)
master.thisListbox.grid(row = 1, column = 0, columnspan = 3, padx = 2, pady = 2)
# Entry
self.entry = ttk.Entry(self)
self.entry.grid(row = 2, column = 0, padx = 2, pady = 2)
# Buttons
self.addButton = ttk.Button(self, text = "Add", width = 4, command = self.Add)
self.addButton.grid(row = 2, column = 1, padx = 2, pady = 2)
self.deleteButton = ttk.Button(self, text = "Delete", width = 6, command = self.Delete)
self.deleteButton.grid(row = 2, column = 2, padx = 2, pady = 2)
for v in values:
master.thisListbox.insert(END, v)
def Add(self):
if self.entry.get() == "": return
master.thisListbox.insert(END, self.entry.get())
self.entry.delete(0, END)
def Delete(self):
for index in reversed(master.thisListbox.curselection()):
master.thisListbox.delete(index)
# listbox.config(height = listbox.size())
Related
I have a problem regarding tkinter's grid_forget() method. I have 2 pages in a notebook and I want to show certain widgets on the second page based off the user's selected options in the first page, I have a multiple choice menu and the grid_forget() method seems to work well until I select 2 or more options from the menu. I tried creating the widgets when an option is selected and place them based on the choice, no luck there, also tried creating them with the rest of the widgets and when the user selected an option I would simply just use grid to place them on the screen, also no luck there. I created a demo below, sorry for potential mistakes.
import tkinter as tk
from tkinter import ttk
SMALL_FONT = ("calibri", 16)
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.notebook = ttk.Notebook(self, height = "900", width = "1600")
self.notebook.grid(row = 0, column = 0)
self.frame_pasul3 = tk.Frame(self.notebook)
self.frame_pasul1 = tk.Frame(self.notebook)
self.notebook.add(self.frame_pasul1, text = "First page")
self.notebook.add(self.frame_pasul3, text = "Second page")
self.first_page()
def first_page(self):
options = ["Urmarire mobiliara",
"Urmarire imobiliara",
"Predarea silita bunuri imobile",
"Predarea silita bunuri mobile",
"Obligatia de a face",
"Executare minori"]
menubutton_modalitate_exec = tk.Menubutton(self.frame_pasul1, text="Alegeti o modalitate de executare",
indicatoron=True, borderwidth=1, fg = "#000000",relief="raised")
menu_modalitate_exec = tk.Menu(menubutton_modalitate_exec, tearoff=False)
menubutton_modalitate_exec.configure(menu=menu_modalitate_exec)
menubutton_modalitate_exec.grid(row = 4, column = 1)
self.modalitate = {}
for choice in options:
self.modalitate[choice] = tk.StringVar()
menu_modalitate_exec.add_checkbutton(label=choice, variable=self.modalitate[choice],
onvalue=1, offvalue=0,
command=self.printValues)
self.second_page()
def second_page(self):
self.frame3_titlu_exec = ttk.Frame(self.frame_pasul3)
self.frame3_titlu_exec.grid()
self.frame3_text = ttk.Frame(self.frame_pasul3)
self.frame3_text.grid()
self.frame3_creante = tk.Frame(self.frame_pasul3)
self.frame3_creante.grid()
self.frame3_reprezentand_obligatia = tk.Frame(self.frame_pasul3)
self.frame3_judecatorie = ttk.Frame(self.frame_pasul3)
self.frame3_judecatorie.grid()
self.frame3_texte = tk.Frame(self.frame_pasul3)
self.frame3_texte.grid()
ttk.Label(self.frame3_titlu_exec, font = SMALL_FONT, text = "Titlu Executoriu").grid(row = 0, column = 0, columnspan = 4 ,pady = 10)
ttk.Button(self.frame3_titlu_exec, text = "Contract de credit.").grid(row = 1, column = 0, padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Sentinta civila.").grid(row = 1, column = 1, padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Contract notarial.").grid(row = 1, column = 2,padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Act de adjudecare.").grid(row = 1, column = 3, padx = 10, ipadx = 15, ipady = 5)
self.entry = tk.Text(self.frame3_text, height = 2, wrap = "word", font = ("Helvetica", 10))
self.entry.grid(row = 0, column = 1, padx = 10, pady = 15)
ttk.Button(self.frame3_text, text = "Incarca titlu").grid(row = 0, column = 2, padx = 10, pady = 15)
ttk.Label(self.frame3_creante, font = SMALL_FONT, text = "Creante").grid(row = 0, column = 0)
self.btn_adauga_creante = ttk.Button(self.frame3_creante, text = "Adauga")
self.btn_adauga_creante.grid(row = 0, column = 3)
self.reprezentand_fapt_label = ttk.Label(self.frame3_judecatorie, font = SMALL_FONT, text = "Ce reprezinta fapta.")
self.reprezentand_fapt_label.grid(row = 2, column = 1)
self.reprezentand_creante = tk.Text(self.frame3_judecatorie, height = 3, width = 70)
self.reprezentand_creante.grid(row = 3 , column = 1, pady = 15)
ttk.Label(self.frame3_texte, font = SMALL_FONT, text = "Judecatorie").grid(row = 4, column = 1)
options_jud = ["optiunea 2.",
"test 3",
"test 4",
"test 5"]
self.judecatorie = ttk.Combobox(self.frame3_texte, values = options_jud)
self.judecatorie.set("Selecteaza o judecatorie.")
self.judecatorie.grid(row = 5, column = 1)
ttk.Button(self.frame3_texte, text = "Pasul 2. Parti dosar.").grid(row = 6, column = 0, padx = 15, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_texte, text = "Pasul 4. Cheltuieli de executare").grid(row = 6, column = 3, ipadx = 15, ipady = 5)
def printValues(self):
for name, var in self.modalitate.items():
if var.get() == "1" and (name == "Predarea silita bunuri imobile" or name == "Predarea silita bunuri mobile" or name == "Obligatia de a face" or name == "Executare minori"):
self.reprezentand_creante_label = tk.Label(self.frame3_judecatorie, font = SMALL_FONT, text = "Ce reprezinta creanta.")
self.reprezentand_creante = tk.Text(self.frame3_judecatorie, wrap = "word", height = 3, width = 70)
self.ok_modalitate_exec = 1
self.reprezentand_creante_label.grid(row = 0, column = 1, padx = 15, pady = 15)
self.reprezentand_creante.grid(row = 1, column = 1, padx = 15, pady = 15)
print("Avem 1 la cele 4", name, var.get())
break
elif var.get() == "0" and (name == "Predarea silita bunuri imobile" or name == "Predarea silita bunuri mobile" or name == "Obligatia de a face" or name == "Executare minori"):
print("Avem 0 la cele 4", name, var.get())
self.ok_modalitate_exec = 0
self.reprezentand_creante_label.grid_forget()
self.reprezentand_creante.grid_forget()
break
if __name__ == "__main__":
main_window = tk.Tk()
app = App(main_window)
app.grid()
main_window.mainloop()
I'm trying to create an app that has 2 windows. The first window (A) is only called the first time the app is ran, and then opens window (B). For all future events, only window (B) is called. This is the following code:
# Script Counter/Setup
def read_counter():
if path.exists("counter.json"):
return loads(open("counter.json", "r").read()) + 1
else:
info = tk.Tk()
info.title("Setup")
info.geometry("350x120")
info.grid_columnconfigure((0, 2), weight = 1)
count = tk.Label(info, text = "Previous Post Number")
string = tk.StringVar()
count_input = tk.Entry(info, textvariable = string)
val = string.get()
def destroy_get():
val = int(count_input.get())
info.quit()
return val
count_button = tk.Button(info, text = "Done!", command = destroy_get)
tk.Label(info, text = "Setup", font='Helvetica 18 bold').grid(row = 0, column = 1, padx = 5, pady = 5)
count.grid(row = 1, column = 0, padx = 5, pady = 5)
count_input.grid(row = 1, column = 2, padx = 5, pady = 5)
count_button.grid(row = 2, column = 1, padx = 5, pady = 5)
info.mainloop()
# info.destroy()
return destroy_get()
def write_counter():
with open("counter.json", "w") as f:
f.write(dumps(counter))
counter = read_counter()
atexit.register(write_counter)
folders = ["to_post/", "not_posted/", "posted"]
for folder in folders:
if not path.exists(folder):
os.mkdir(folder)
print(os.getcwd())
# GUI
window = tk.Tk()
window.title("Confessions Bot")
window.geometry("600x350")
window.grid_columnconfigure((0,2), weight = 1)
label_tell_account = tk.Label(window, text = "Tellonym Account")
label_tell_password = tk.Label(window, text = "Tellonym Password")
label_ig_account = tk.Label(window, text = "Instagram Account")
label_ig_password = tk.Label(window, text = "Instagram Password")
tell_account = tk.Entry(window)
tell_password = tk.Entry(window)
ig_account = tk.Entry(window)
ig_password = tk.Entry(window)
image = ImageTk.PhotoImage(Image.open("logo.png"))
tk.Label(window, image = image).grid(row = 0, column = 1, padx = 10, pady = 10)
label_tell_account.grid(row = 1, column = 0)
tell_account.grid(row = 1, column = 2, padx = 10, pady = 10)
label_tell_password.grid(row = 2, column = 0, padx = 10, pady = 10)
tell_password.grid(row = 2, column = 2, padx = 10, pady = 10)
label_ig_account.grid(row = 3, column = 0, padx = 10, pady = 10)
ig_account.grid(row = 3, column = 2, padx = 10, pady = 10)
label_ig_password.grid(row = 4, column = 0, padx = 10, pady = 10)
ig_password.grid(row = 4, column = 2, padx = 10, pady = 10)
# run.grid(row = 5, column = 1, padx = 10, pady = 10)
window.mainloop()
When this is ran I get _tkinter.TclError: image "pyimage1" doesn't exist. I read that this happens since I haven't destroyed my initial window. If I change destroy_get() to have info.destroy() I'm no longer able to get the the entry from window A.
This is my first time using Tkinter and coding in general so any help is welcome.
I am trying to call a variable or a StringVar from the class called GUI without calling the GUI as the object of the class Conversion. Here is an example of my code:
class GUI:
def __init__(self,root):
c = Conversion(measurements,measurements1,Text_Legnth_Left,Text_Length_Right)
measurements = self.measurements
measurements1 = self.measurements1
Text_Length_Left = self.Text_Legnth_Left
Text_Length_Right = self.Text_Length_Right
self.notebook = ttk.Notebook(root)
self.notebook.pack()
self.length_frame = ttk.Frame(self.notebook)
self.weight_frame = ttk.Frame(self.notebook)
self.temperature_frame = ttk.Frame(self.notebook)
self.notebook.add(self.length_frame, text = 'Length')
#Combobox
self.measurements = StringVar()
self.Combobox_Length_Left = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state=['readonly'])
self.Combobox_Length_Left.current(5)
self.measurements.trace('w', c.update_length())
self.Combobox_Length_Left.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = E)
self.measurements1 = StringVar()
self.Combobox_Length_Right = ttk.Combobox(self.length_frame, textvariable = self.measurements1, value = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state = ['readonly'])
self.Combobox_Length_Right.current(5)
self.measurements1.trace('w', c.self.update_length())
self.Combobox_Length_Right.grid(row = 2, column = 2, padx = 5, pady = 5, sticky = E)
#Labels
self.Conversion = ttk.Label(self.length_frame, text = 'Conversion:').grid(row = 1, column = 0, padx = 5, pady = 5, sticky = W)
self.Label_Blank = ttk.Label(self.length_frame, text = '').grid(row = 1, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To2 = ttk.Label(self.length_frame, text = 'to').grid(row = 2, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To = ttk.Label(self.length_frame, text = 'to').grid(row = 3, column = 1, padx = 5, pady = 5, sticky = E)
#Entry Boxes
self.Text_Length_Left = StringVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Text_Length_Left.trace('w', self.update_length)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)
self.Text_Length_Right = StringVar()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13, state='readonly')
And here is my other class:
class Conversion:
def __init__(self,measurements,measurements1,Text_Legnth_Left,Text_Length_Right):
g = GUI(measurements,measurements1,Text_Legnth_Left,Text_Length_Right)
as_meter = dict(mm=0.001, cm=0.01, inch=0.0254,ft=0.3048, yd=0.9144, m=1.0,km=1000.0, mi=1609.344,)
def update_length(self, *args,g):
try:
v = int(g.self.Text_Length_Left.get())
except ValueError:
g.self.Text_Length_Left.set('')
g.self.Text_Length_Right.set('')
return
m = v * self.as_meter [g.self.measurements.get()]
r = m/self.as_meter[g.self.measurements1.get()]
g.self.Text_Length_Right.set("{:.5g}".format(r))
My attempt of calling the the variables such as measurements was c=Conversion(measurements) I know by doing class GUI(Conversion) will work and make it its object but I can't do it for specific reasons.
Thanks for any help really appreciate it!
I'm working on my final project for my computing I class.
The problem that I am having is:
When I click on the new entry button, hit the back button and click on the new entry button once again it does not work.
If you guys could tell me why that is?
The command on the button seems to be only working once. Thanks for your help.
Code:
from tkinter import *
import tkinter.filedialog
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.title("Entry Sheet")
self.font = ("Helvetica","13")
self.header_font = ("Helvetica","18")
self.exercise_font = ("Helvetica","13","bold")
self.delete = 'a'
self.new_user()
def new_user(self):
if self.delete == 'b':
self.delete = 'c'
self.hide()
self.delete = 'b'
self.new_entry = Button(self, text = 'New Entry', command = self.entry, width = 15)
self.new_entry.grid(row = 1, column = 0, columnspan = 3, padx = 10, pady = 5)
self.look_entry = Button(self, text = 'See Entries', command = self.see_entries, width = 15)
self.look_entry.grid(row = 2, column =0, columnspan = 3, padx = 10, pady = 5)
def entry(self):
print(1)
self.delete = 'b'
self.hide()
self.entry = Label(self, text = 'New Entry', font = self.header_font)
self.entry.grid(row = 0, column = 0, columnspan = 2)
self.numberlbl = Label(self, text = 'Please choose a muscle?', font = self.font)
self.numberlbl.grid(row = 1, column= 0, columnspan = 2, sticky = 'w' )
self.muscle_chosen = IntVar()
self.chest = Radiobutton(self, text = "chest", variable = self.muscle_chosen, value = 1, font = self.font)
self.bicep = Radiobutton(self, text = "bicep", variable = self.muscle_chosen, value = 2, font = self.font)
self.chest.grid(row = 2, column = 0)
self.bicep.grid(row = 2, column = 1)
self.exerciseslbl = Label(self, text = 'Please enter the number of exercises: ', font = self.font)
self.exerciseslbl.grid(row = 3, column = 0, columnspan = 3)
self.exercises_spinbox = Spinbox(self, from_= 1, to_= 50, width = 5, font = self.font)
self.exercises_spinbox.grid(row = 4, column = 0)
self.back_button = Button(self, text = 'Back', command = self.new_user, width = 10)
self.back_button.grid(row =5, column=0, pady =10)
def see_entries(self):
print("Goes through")
def hide(self):
if self.delete == 'b':
self.new_entry.grid_remove()
self.look_entry.grid_remove()
elif self.delete == 'c':
self.entry.grid_remove()
self.numberlbl.grid_remove()
self.chest.grid_remove()
self.bicep.grid_remove()
self.exerciseslbl.grid_remove()
self.exercises_spinbox.grid_remove()
self.back_button.grid_remove()
def main():
app = App()
app.mainloop()
if __name__=="__main__":
main()
In your entry function you overwrite self.entry, which is the name of the function, with a reference to a Label. When the button then calls self.entry it isn't function.
Simply call the Label something else.
I'm pretty new to Python and have written my first ever program with a GUI and have been reading a lot about code to loop your way into creating multiple widgets but haven't seen a solution for my code below.
I have written a program that simulates a tennis match and have created a GUI for the program which consists of GUI in the form as a Livescoreboard.
For the scoreboard I have createad a lot of labels that will contain the different scores and the players names etc like a real tennis scoreboard and I've also made a few buttons.
My code below is the two methods in which I create and update all the widgets in the GUI. Looking at it, from a novice perspective, it doesn't seem to be the best way of writing this code.
Have been searching and trying to come up with something else than this but haven't found any.
Is there a way to this better or is it fine the way it is?
# Creates the widgets in the GUI.
def create_widgets(self):
# Images
photo = tk.PhotoImage(file = 'Federer.gif')
label = tk.Label(self, image = photo)
label.image = photo
label.place(relwidth = 1, relheight = 1)
img = tk.PhotoImage(file = 'Rolex.gif')
rolex_label = tk.Label(self, image = img)
rolex_label.image = img
rolex_label.grid(row = 0, column = 3, columnspan = 2)
# Creates and sets some StringVar()
self.set_1_player_1 = tk.StringVar()
self.set_1_player_2 = tk.StringVar()
self.set_2_player_1 = tk.StringVar()
self.set_2_player_2 = tk.StringVar()
self.set_3_player_1 = tk.StringVar()
self.set_3_player_2 = tk.StringVar()
self.spelare_1 = tk.StringVar()
self.spelare_2 = tk.StringVar()
self.sets_player_1 = tk.StringVar()
self.sets_player_2 = tk.StringVar()
self.games_player_1 = tk.StringVar()
self.games_player_2 = tk.StringVar()
self.points_player_1 = tk.StringVar()
self.points_player_2 = tk.StringVar()
self.show_score_by = tk.StringVar()
self.spelare_1.set(self.prepared_match.get_player_1().get_name())
self.spelare_2.set(self.prepared_match.get_player_2().get_name())
self.sets_player_1.set('0')
self.sets_player_2.set('0')
self.games_player_1.set('0')
self.games_player_2.set('0')
self.points_player_1.set('0')
self.points_player_2.set('0')
self.show_score_by.set(None)
# The labels
tk.Label(self, font = "Purisa 8 bold", text = 'Set 1', height = 2, width = 6, relief = tk.RAISED).grid(row = 0, column = 0)
tk.Label(self, font = "Purisa 8 bold", text = 'Set 2', height = 2, width = 6, relief = tk.RAISED).grid(row = 0, column = 1)
tk.Label(self, font = "Purisa 8 bold", text = 'Set 3', height = 2, width = 6, relief = tk.RAISED).grid(row = 0, column = 2)
tk.Label(self, font = "Purisa 8 bold", text = 'SETS', height = 2, width = 6, relief = tk.RAISED).grid(row = 0, column = 5)
tk.Label(self, font = "Purisa 8 bold", text = 'GAMES', height = 2, width = 6, relief = tk.RAISED).grid(row = 0, column = 6)
tk.Label(self, font = "Purisa 8 bold", text = 'POINTS', height = 2, width = 15, relief = tk.RAISED).grid(row = 0, column = 7)
tk.Label(self, textvariable = self.set_1_player_1, height = 2, width = 6, relief = tk.RAISED).grid(row = 1, column = 0)
tk.Label(self, textvariable = self.set_2_player_1, height = 2, width = 6, relief = tk.RAISED).grid(row = 1, column = 1)
tk.Label(self, textvariable = self.set_3_player_1, height = 2, width = 6, relief = tk.RAISED).grid(row = 1, column = 2)
self.name_player_1 = tk.Label(self, textvariable = self.spelare_1, height = 2, width = 18, relief = tk.RAISED)
self.name_player_1.grid(row = 1, column = 3, columnspan = 2)
tk.Label(self, textvariable = self.sets_player_1, height = 2, width = 6, relief = tk.RAISED).grid(row = 1, column = 5)
tk.Label(self, textvariable = self.games_player_1, height = 2, width = 6, relief = tk.RAISED).grid(row = 1, column = 6)
tk.Label(self, textvariable = self.points_player_1, height = 2, width = 15, relief = tk.RAISED).grid(row = 1, column = 7)
tk.Label(self, textvariable = self.set_1_player_2, height = 2, width = 6, relief = tk.RAISED).grid(row = 2, column = 0)
tk.Label(self, textvariable = self.set_2_player_2, height = 2, width = 6, relief = tk.RAISED).grid(row = 2, column = 1)
tk.Label(self, textvariable = self.set_3_player_2, height = 2, width = 6, relief = tk.RAISED).grid(row = 2, column = 2)
self.name_player_2 = tk.Label(self, textvariable = self.spelare_2, height = 2, width = 18, relief = tk.RAISED)
self.name_player_2.grid(row = 2, column = 3, columnspan = 2)
tk.Label(self, textvariable = self.sets_player_2, height = 2, width = 6, relief = tk.RAISED).grid(row = 2, column = 5)
tk.Label(self, textvariable = self.games_player_2, height = 2, width = 6, relief = tk.RAISED).grid(row = 2, column = 6)
tk.Label(self, textvariable = self.points_player_2, height = 2, width = 15, relief = tk.RAISED).grid(row = 2, column = 7)
tk.Label(self, font = "Purisa 8 bold", text = 'Show score by: ', height = 2, width = 15).grid(row = 0, column = 8, padx = 50)
# The buttons
self.play_button = tk.Button(self, bd = 5, text = 'Play', command = self.play, height = 2, width = 10, relief = tk.RIDGE)
self.play_button.grid(pady = 10, row = 3, column = 3, columnspan = 2)
self.quit_button = tk.Button(self, bd = 3, text = 'Quit', command = self.quit, height = 1, width = 5, relief = tk.RIDGE)
self.quit_button.grid(row = 5, column = 3, columnspan = 2, pady = 5)
self.restart_button = tk.Button(self, bd = 4, text = 'Restart', command = self.restart, height = 1, width = 7, relief = tk.RIDGE)
self.restart_button.grid(row = 4, column = 3, columnspan = 2, pady = 5)
self.pause_button = tk.Button(self, text = 'Pause', command = self.pause, height = 1, width = 5, relief = tk.RIDGE)
self.pause_button.grid(row = 3, column = 8, sticky = tk.S)
tk.Radiobutton(self, command = self.show_score, text = 'Point', variable = self.show_score_by, value = 'Point', height = 1, width = 10).grid(row = 1, column = 8, padx = 50, sticky = tk.S)
tk.Radiobutton(self, command = self.show_score, text = 'Game', variable = self.show_score_by, value = 'Game', height = 1, width = 10).grid(row = 2, column = 8, padx = 50)
tk.Radiobutton(self, command = self.show_score, text = 'Set', variable = self.show_score_by, value = 'Set', height = 1, width = 10).grid(row = 3, column = 8, padx = 50, sticky = tk.N)
# Updates the widgets in the frame.
def update_widgets(self):
self.sets_player_1.set(self.prepared_match.get_sets()[0])
self.sets_player_2.set(self.prepared_match.get_sets()[1])
if self.prepared_match.show_score_by in ['Point', 'Game', None]:
self.games_player_1.set(self.prepared_match.get_games()[0])
self.games_player_2.set(self.prepared_match.get_games()[1])
if self.prepared_match.show_score_by == 'Point':
self.points_player_1.set(self.prepared_match.get_real_score()[0])
self.points_player_2.set(self.prepared_match.get_real_score()[1])
elif self.prepared_match.show_score_by == 'Game':
self.points_player_1.set('0')
self.points_player_2.set('0')
elif self.prepared_match.show_score_by == 'Set':
self.points_player_1.set('0')
self.points_player_2.set('0')
self.games_player_1.set('0')
self.games_player_2.set('0')
self.set_1_player_1.set(self.prepared_match.get_previous_sets()[0][0])
self.set_1_player_2.set(self.prepared_match.get_previous_sets()[0][1])
self.set_2_player_1.set(self.prepared_match.get_previous_sets()[1][0])
self.set_2_player_2.set(self.prepared_match.get_previous_sets()[1][1])
self.set_3_player_1.set(self.prepared_match.get_previous_sets()[2][0])
self.set_3_player_2.set(self.prepared_match.get_previous_sets()[2][1])
if self.prepared_match.match_is_ended():
if self.prepared_match.get_sets()[0] == 2:
self.name_player_1.config(fg = 'red')
else:
self.name_player_2.config(fg = 'red')
self.update()
You can make use of setattr (and as you made a class, the __ setattr __ method). You can refactor a lot of your code but you'll find hereunder optimizations your initialization code :
You can create a method like :
def create_scoreboard(self, n_players, n_sets):
""" Create attributes set_X_player_Y """
for player in range(1, n_players+1):
for set in range(1, n_sets+1):
self.__setattr__(
"set_%d_player_%d" % (set, player),
tk.StringVar()
)
for attribute in ['sets', 'games', 'points']:
self.__setattr__(
"%s_player_%d" % (attribute, player),
tk.StringVar(value='0')
)
Then you can loop over players, sets, etc.
However, the best approach would be to create player objects to store their scores. You can try to isolate objects using a "natural" approach and creating a scoreboard object, players, gamepads (for your buttons), etc.