diff --git a/README.md b/README.md index fa0237d..d1eb93b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # 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. diff --git a/src/exerciseten/radiobutton.py b/src/exerciseten/radiobutton.py new file mode 100644 index 0000000..ba90e43 --- /dev/null +++ b/src/exerciseten/radiobutton.py @@ -0,0 +1,115 @@ +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 10.A') + + # # Max and min window size + master.maxsize(width=400, height=400) + master.minsize(width=220, height=220) + + # Variable + 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(True) + self._main.pack(fill='both', expand=True) + 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) + 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 _cambiarLabel(self): + """Method to change label text + :return: None + :rtype: NoneType + """ + self._lb.config(text=self._rbv.get()) + + def _reiniciar(self): + """Method to restore radio button selections + :return: None + :rtype: NoneType + """ + self._rbv.set(' ') + + def _createWidgets(self): + """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='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 and label + # # Init button and label + self._bt = tk.Button(self._fb, command=lambda: self._reiniciar(), text='Reiniciar') + 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]: + """Main function to start Application class + :return: Application class, None when error exist + :rtype: tuple[tk.TclError, Application or None] + """ + # 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..6fda939 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 ten.A :return: None :rtype: NoneType """ - pass + state, app = mainApp() + exit(state) if __name__ == '__main__':