From daee582ecc69346fc227f989e4a92ccb420da229 Mon Sep 17 00:00:00 2001 From: Izri Toufali Lapaz Date: Tue, 15 Nov 2022 18:43:51 +0100 Subject: [PATCH 1/5] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index fa0237d..263390f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # OpenBootCampPython Repository to store Python boot camp. + +* Ejercicio 10.A: En este ejercicio tenéis que crear una lista de RadioButton que muestre la opción que se ha seleccionado y que contenga un botón de reinicio para que deje todo como al principio. + +Al principio no tiene que haber una opción seleccionada. From 86ecaf810e88d45405dd01d71fa7fb949268cd88 Mon Sep 17 00:00:00 2001 From: tlizri Date: Wed, 16 Nov 2022 13:16:32 +0100 Subject: [PATCH 2/5] Completed exercise ten.A --- src/exerciseten/radiobutton.py | 101 +++++++++++++++++++++++++++++++++ src/main.py | 8 ++- 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/exerciseten/radiobutton.py diff --git a/src/exerciseten/radiobutton.py b/src/exerciseten/radiobutton.py new file mode 100644 index 0000000..892485b --- /dev/null +++ b/src/exerciseten/radiobutton.py @@ -0,0 +1,101 @@ +import tkinter as tk + + +class Application: + """Conceptual class for GUI application + :param master: main window of an application, default to None + :type master: tkinter.Tk + """ + + def __init__(self, master: tk.Tk = None): + """Constructor method + """ + # Parent setting + # # Size of the window + master.geometry('420x420') + + # # Title of the window + master.title('Ejercicio 9.A') + + # # Max and min window size + master.maxsize(width=400, height=400) + master.minsize(width=220, height=220) + + # Variable + self._rbv = tk.IntVar() + + # Widgets + self._rb1 = tk.Radiobutton() + self._rb2 = tk.Radiobutton() + self._rb3 = tk.Radiobutton() + self._bt = tk.Button() + + # Frame + # # Init main frame + self._main = tk.Frame(master=master, height=420, width=420) + self._main.grid_propagate(False) + self._main.pack(fill='both', expand=True) + self._main.rowconfigure(index=0, weight=1) + self._main.columnconfigure(index=0, weight=1) + + # # Init button frame + self._fb = tk.Frame(master=self._main, height=1, width=1) + self._fb.pack(side='bottom', expand=False) + self._fb.rowconfigure(index=0, weight=1) + self._fb.columnconfigure(index=0, weight=1) + + # # Init radio buttons frame + self._frb = tk.Frame(master=self._main, height=1, width=1) + self._frb.pack(side='left', expand=False) + self._frb.rowconfigure(index=0, weight=1) + self._frb.columnconfigure(index=0, weight=1) + + # Place widgets into frames + self._createWidgets() + + # Start application + master.mainloop() + + def _reiniciar(self): + """Method to restore radio button selections + :return: None + :rtype: NoneType + """ + self._rbv.set(False) + + def _createWidgets(self): + """Method to instance and place button widgets + :return: None + :rtype: NoneType + """ + # Radio buttons + # # Init radio buttons + self._rb1 = tk.Radiobutton(self._frb, text='Opcion 1', value=1, variable=self._rbv) + self._rb2 = tk.Radiobutton(self._frb, text='Opcion 2', value=2, variable=self._rbv) + self._rb3 = tk.Radiobutton(self._frb, text='Opcion 3', value=3, variable=self._rbv) + # # Place radio buttons + self._rb1.grid(column=0, row=0, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.W + tk.N) + self._rb2.grid(column=0, row=1, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.W + tk.N) + self._rb3.grid(column=0, row=2, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.W + tk.N) + + # Button + # # Init button + self._bt = tk.Button(self._fb, command=lambda: self._reiniciar(), text='Reiniciar') + # # Place button + self._bt.grid(column=0, row=0, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.S) + + +def mainApp() -> tuple[int, Application or None]: + """Main function to start Application class + :return: Application class, None when error exist + :rtype: Application + """ + # Main window of an application + root = tk.Tk() + + try: + # Class instance + app = Application(master=root) + return 0, app + except tk.TclError: + return 1, None diff --git a/src/main.py b/src/main.py index a47b6a3..4fb2aec 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,13 @@ +from exerciseten.radiobutton import mainApp + + def main(): - """Main script for pythonBootCamp project + """Main script for exercise nine.A :return: None :rtype: NoneType """ - pass + state, app = mainApp() + exit(state) if __name__ == '__main__': From d7f49b40c744723f5e4a46d6e0db4d3bba10f0df Mon Sep 17 00:00:00 2001 From: Izri Toufali Lapaz Date: Wed, 16 Nov 2022 13:18:36 +0100 Subject: [PATCH 3/5] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 263390f..d1eb93b 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,4 @@ Repository to store Python boot camp. * Ejercicio 10.A: En este ejercicio tenéis que crear una lista de RadioButton que muestre la opción que se ha seleccionado y que contenga un botón de reinicio para que deje todo como al principio. - Al principio no tiene que haber una opción seleccionada. From 2e2197884a22c7c04c2a277c9a3e6d4e78e22b64 Mon Sep 17 00:00:00 2001 From: tlizri Date: Thu, 17 Nov 2022 18:29:11 +0100 Subject: [PATCH 4/5] Added label with the text of the selected radiobutton --- src/exerciseten/radiobutton.py | 36 ++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/exerciseten/radiobutton.py b/src/exerciseten/radiobutton.py index 892485b..2501b1c 100644 --- a/src/exerciseten/radiobutton.py +++ b/src/exerciseten/radiobutton.py @@ -22,21 +22,22 @@ def __init__(self, master: tk.Tk = None): master.minsize(width=220, height=220) # Variable - self._rbv = tk.IntVar() + self._rbv = tk.StringVar(value=' ') # Widgets self._rb1 = tk.Radiobutton() self._rb2 = tk.Radiobutton() self._rb3 = tk.Radiobutton() self._bt = tk.Button() + self._lb = tk.Label() # Frame # # Init main frame self._main = tk.Frame(master=master, height=420, width=420) - self._main.grid_propagate(False) + self._main.grid_propagate(True) self._main.pack(fill='both', expand=True) - self._main.rowconfigure(index=0, weight=1) - self._main.columnconfigure(index=0, weight=1) + self._main.rowconfigure(index=0, weight=420) + self._main.columnconfigure(index=0, weight=420) # # Init button frame self._fb = tk.Frame(master=self._main, height=1, width=1) @@ -56,33 +57,42 @@ def __init__(self, master: tk.Tk = None): # Start application master.mainloop() + def _cambiarLabel(self): + self._lb.config(text=self._rbv.get()) + def _reiniciar(self): """Method to restore radio button selections :return: None :rtype: NoneType """ - self._rbv.set(False) + self._rbv.set(' ') def _createWidgets(self): - """Method to instance and place button widgets + """Method to instance and place buttons and label widgets :return: None :rtype: NoneType """ # Radio buttons # # Init radio buttons - self._rb1 = tk.Radiobutton(self._frb, text='Opcion 1', value=1, variable=self._rbv) - self._rb2 = tk.Radiobutton(self._frb, text='Opcion 2', value=2, variable=self._rbv) - self._rb3 = tk.Radiobutton(self._frb, text='Opcion 3', value=3, variable=self._rbv) + self._rb1 = tk.Radiobutton(self._frb, text='Opción 1', value='Opción 1', + variable=self._rbv, command=lambda: self._cambiarLabel()) + self._rb2 = tk.Radiobutton(self._frb, text='Opción 2', value='Opción 2', + variable=self._rbv, command=lambda: self._cambiarLabel()) + self._rb3 = tk.Radiobutton(self._frb, text='Opción 3', value='Opción 3', + variable=self._rbv, command=lambda: self._cambiarLabel()) + # # Place radio buttons self._rb1.grid(column=0, row=0, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.W + tk.N) self._rb2.grid(column=0, row=1, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.W + tk.N) self._rb3.grid(column=0, row=2, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.W + tk.N) - # Button - # # Init button + # Button and label + # # Init button and label self._bt = tk.Button(self._fb, command=lambda: self._reiniciar(), text='Reiniciar') - # # Place button - self._bt.grid(column=0, row=0, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.S) + self._lb = tk.Label(self._fb, textvariable=self._rbv) + # # Place button and label + self._lb.grid(column=0, row=0, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.N) + self._bt.grid(column=0, row=1, ipadx=0, ipady=0, padx=10, pady=5, sticky=tk.S) def mainApp() -> tuple[int, Application or None]: From 13cf73e9ece9d0241a519c71bf730483d622ab41 Mon Sep 17 00:00:00 2001 From: tlizri Date: Thu, 17 Nov 2022 19:32:44 +0100 Subject: [PATCH 5/5] Fixed window title and some docstrings --- src/exerciseten/radiobutton.py | 8 ++++++-- src/main.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/exerciseten/radiobutton.py b/src/exerciseten/radiobutton.py index 2501b1c..ba90e43 100644 --- a/src/exerciseten/radiobutton.py +++ b/src/exerciseten/radiobutton.py @@ -15,7 +15,7 @@ def __init__(self, master: tk.Tk = None): master.geometry('420x420') # # Title of the window - master.title('Ejercicio 9.A') + master.title('Ejercicio 10.A') # # Max and min window size master.maxsize(width=400, height=400) @@ -58,6 +58,10 @@ def __init__(self, master: tk.Tk = None): master.mainloop() def _cambiarLabel(self): + """Method to change label text + :return: None + :rtype: NoneType + """ self._lb.config(text=self._rbv.get()) def _reiniciar(self): @@ -98,7 +102,7 @@ def _createWidgets(self): def mainApp() -> tuple[int, Application or None]: """Main function to start Application class :return: Application class, None when error exist - :rtype: Application + :rtype: tuple[tk.TclError, Application or None] """ # Main window of an application root = tk.Tk() diff --git a/src/main.py b/src/main.py index 4fb2aec..6fda939 100644 --- a/src/main.py +++ b/src/main.py @@ -2,7 +2,7 @@ def main(): - """Main script for exercise nine.A + """Main script for exercise ten.A :return: None :rtype: NoneType """