diff --git a/.gitignore b/.gitignore index 0e4d58d..16014e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ __pycache__/ -interm/files/*.txt -interm/files/*.csv +*/files/*.txt +*/files/*.csv diff --git a/basic/basic01.py b/basic/basic01.py index 8819fec..738e79c 100644 --- a/basic/basic01.py +++ b/basic/basic01.py @@ -6,7 +6,7 @@ class basics_001: print(f"Hola {nombre}") def hola2(self): - """Ask for the user’s first name and then ask for their surname + """Ask for the user's first name and then ask for their surname and display the output message Hello [First Name] [Surname].""" nombre = input("Ingresa tu primer nombre: ") apellido = input("Ingresa tu apellido: ") diff --git a/basic/basic02.py b/basic/basic02.py index dbc6673..17bac64 100644 --- a/basic/basic02.py +++ b/basic/basic02.py @@ -35,7 +35,7 @@ class basics_002(): def fav_color(self): """Ask the user to enter their favourite colour. If they enter “red”, “RED” or “Red” display the message “I like red too”, otherwise display the message - “I don’t like [colour], I prefer red”.""" + “I don't like [colour], I prefer red”.""" color = input("¿Cual es tu color favorito?: ") match color.lower(): case "rojo": @@ -45,7 +45,7 @@ class basics_002(): def clima(self): """Ask the user if it is raining and convert their answer to lower case - so it doesn’t matter what case they type it in. If they answer “yes”, + so it doesn't matter what case they type it in. If they answer “yes”, ask if it is windy. If they answer “yes” to this second question, display the answer “It is too windy for an umbrella”, otherwise display the message “Take an umbrella”. If they did not answer yes @@ -61,7 +61,7 @@ class basics_002(): print("Disfruta del día") def edad_conducir(self): - """Ask the user’s age. If they are 18 or over, display the message \"You + """Ask the user's age. If they are 18 or over, display the message \"You can vote\", if they are aged 17, display the message \"You can learn to drive\", if they are 16, display the message \"You can buy a lottery ticket\", if they are under 16, display the message \"You can go diff --git a/basic/basic05.py b/basic/basic05.py index 00b41a5..6757348 100644 --- a/basic/basic05.py +++ b/basic/basic05.py @@ -65,7 +65,7 @@ class basics_005: """Set a variable called total to 0. Ask the user to enter five numbers and after each input ask them if they want that number included. If they do, then add the number to the total. If they do not want it - included, don’t add it to the total. After they have entered all five + included, don't add it to the total. After they have entered all five numbers, display the total.""" total = 0 for _ in range(5): @@ -80,7 +80,7 @@ class basics_005: select up, then ask them for the top number and then count from 1 to that number. If they select down, ask them to enter a number below 20 and then count down from 20 to that number. If they entered something - other than up or down, display the message \"I don’t understand\".""" + other than up or down, display the message \"I don't understand\".""" from time import sleep toward = input("¿Prefieres contar hacia ARRIBA o hacia ABAJO?: ").lower() if toward == 'arriba': diff --git a/interm/interm01.py b/interm/interm01.py index 24809d1..aad2bef 100644 --- a/interm/interm01.py +++ b/interm/interm01.py @@ -34,7 +34,7 @@ class interm001: def del_subject(self): """Create a list of six school subjects. Ask the user which of these - subjects they don’t like. Delete the subject they have chosen from the + subjects they don't like. Delete the subject they have chosen from the list before you display the list again.""" subjs = ['Redes', 'POO', 'SQL', 'Algoritmos', 'Hardware'] print("Materias: ", ", ".join(subjs)) diff --git a/interm/interm07.py b/interm/interm07.py index c745535..e815dee 100644 --- a/interm/interm07.py +++ b/interm/interm07.py @@ -21,7 +21,7 @@ class interm007: of a number…\' and then ask the user to guess the number they are thinking of. Define a third subprogram that will check to see if the comp_num is the - same as the user’s guess. If it is, it should display the message + same as the user's guess. If it is, it should display the message \'Correct, you win\', otherwise it should keep looping, telling the user if they are too low or too high and asking them to guess again until they guess correctly.""" @@ -56,14 +56,14 @@ class interm007: Enter 1 or 2: If they enter a 1, it should run a subprogram that will generate two random numbers between 5 and 20, and ask the user to add them together. - Work out the correct answer and return both the user’s answer and the + Work out the correct answer and return both the user's answer and the correct answer. If they entered 2 as their selection on the menu, it should run a subprogram that will generate one number between 25 and 50 and another number between 1 and 25 and ask them to work out num1 minus num2. This way they will not have to worry about negative answers. Return both the - user’s answer and the correct answer. - Create another subprogram that will check if the user’s answer matches + user's answer and the correct answer. + Create another subprogram that will check if the user's answer matches the actual answer. If it does, display \'Correct\', otherwise display a message that will say \'Incorrect, the answer is\' and display the real answer. diff --git a/main.py b/main.py index 7e919ab..0729729 100755 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from basic import basic from trtl import trtl from interm import interm +from tkgui import tkgui from common.common import clear, user_input, opcs_default def toc(): @@ -98,7 +99,24 @@ def tkinter_challenges(): 1) Tkinter 124-132 2) Tkinter 133-138 """ - print(content) + select_ok = False + while not select_ok: + clear() + print(content) + opcs_default(1) + selection = user_input(1) + match selection: + case 1: + tkgui.challenges01() + case 2: + #tkgui.challenges02() + pass + case 'v': + return + case 's': + exit(0) + case _: + continue def main(): clear() diff --git a/tkgui/example01.py b/tkgui/example01.py new file mode 100644 index 0000000..4781249 --- /dev/null +++ b/tkgui/example01.py @@ -0,0 +1,13 @@ +import tkinter as tk + +def Call(): + msg = tk.Label(window, text="Presionaste el botón") + msg.place(x=30, y=50) + button["bg"] = "blue" + button["fg"] = "white" + +window = tk.Tk() +window.geometry("200x100") +button = tk.Button(text="Presioname", command=Call) +button.place(x=30, y=20, width=120, height=25) +window.mainloop() diff --git a/tkgui/files/text_files_temp_dir b/tkgui/files/text_files_temp_dir new file mode 100644 index 0000000..6d0255d --- /dev/null +++ b/tkgui/files/text_files_temp_dir @@ -0,0 +1 @@ +directorio para los archivos temporales de escritura y lectura diff --git a/tkgui/sols_124-132/sol_01.py b/tkgui/sols_124-132/sol_01.py new file mode 100644 index 0000000..751d854 --- /dev/null +++ b/tkgui/sols_124-132/sol_01.py @@ -0,0 +1,29 @@ +from tkinter import * + +def click(): + name = txtbx1.get() + msg = f"Hola {name}" + txtbx2["bg"] = "yellow" + txtbx2["fg"] = "blue" + txtbx2["text"] = msg + +window = Tk() +window.geometry("500x200") + +lbl1 = Label(text="Ingresa tu nombre:") +lbl1.place(x=30, y=20) + +txtbx1 = Entry() +txtbx1.place(x=150, y=20, width=200, height=25) +txtbx1["justify"] = "center" +txtbx1.focus() + +btn1 = Button(text="Presioname", command=click) +btn1.place(x=30, y=50, width=120, height=25) + +txtbx2 = Message(text="") +txtbx2.place(x=150, y=50, width=200, height=25) +txtbx2["bg"]="white" +txtbx2["fg"]="black" + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_02.py b/tkgui/sols_124-132/sol_02.py new file mode 100644 index 0000000..8367a69 --- /dev/null +++ b/tkgui/sols_124-132/sol_02.py @@ -0,0 +1,18 @@ +from tkinter import * +import random + +def click(): + num = random.randint(1,6) + answer["text"] = num + +window = Tk() +window.title("Tira un Dado") +window.geometry("100x200") + +button1 = Button(text="Roll", command=click) +button1.place(x=30, y=30, width=50, height=25) + +answer = Message(text="") +answer.place(x=40, y=70, width=30, height=25) + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_03.py b/tkgui/sols_124-132/sol_03.py new file mode 100644 index 0000000..82e30ad --- /dev/null +++ b/tkgui/sols_124-132/sol_03.py @@ -0,0 +1,46 @@ +from tkinter import * + +def add_on(): + num = enter_txt.get() + num = int(num) + answer = output_txt["text"] + answer = int(answer) + total = num + answer + output_txt["text"] = total + +def reset(): + total = 0 + output_txt["text"] = 0 + enter_txt.delete(0, 'end') + enter_txt.focus() + +total = 0 +num = 0 + +window = Tk() +window.title("Agregando números") +window.geometry("450x100") + +enter_lbl = Label(text="Ingresa un número:") +enter_lbl.place(x=50, y=20, width=120, height=25) + +enter_txt = Entry() +enter_txt.place(x=175, y=20, width=120, height=25) +enter_txt["justify"] = "center" +enter_txt.focus() + +add_btn = Button(text="Agregar", command=add_on) +add_btn.place(x=300, y=20, width=55, height=25) + +output_lbl = Label(text="TOTAL = ") +output_lbl.place(x=110, y=50, width=70, height=25) + +output_txt = Message(text=0) +output_txt.place(x=175, y=50, width=120, height=25) +output_txt["bg"] = "white" +output_txt["relief"] = "sunken" + +clear_btn = Button(text="Borrar", command=reset) +clear_btn.place(x=300, y=50, width=55, height=25) + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_04.py b/tkgui/sols_124-132/sol_04.py new file mode 100644 index 0000000..f08d30e --- /dev/null +++ b/tkgui/sols_124-132/sol_04.py @@ -0,0 +1,33 @@ +from tkinter import * + +def add_name(): + name = name_box.get() + name_list.insert('end', name) + name_box.delete(0, 'end') + name_box.focus() + +def clear_list(): + name_list.delete(0, 'end') + name_box.focus() + +window = Tk() +window.title("Lista Nombres") +window.geometry("400x200") + +labell1 = Label(text="Agregar:") +labell1.place(x=20, y=20, width=100, height=25) + +name_box = Entry() +name_box.place(x=120, y=20, width=100, height=25) +name_box.focus() + +button1 = Button(text="Agregar", command=add_name) +button1.place(x=250, y=20, width=100, height=25) + +name_list = Listbox() +name_list.place(x=120, y=50, width=100, height=100) + +button2 = Button(text="Borrar", command=clear_list) +button2.place(x=250, y=50, width=100, height=25) + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_05.py b/tkgui/sols_124-132/sol_05.py new file mode 100644 index 0000000..27d8c84 --- /dev/null +++ b/tkgui/sols_124-132/sol_05.py @@ -0,0 +1,41 @@ +from tkinter import * + +def convert_km_to_ml(): + km = textbox1.get() + km = int(km) + message = km * 0.6214 + textbox2.delete(0, 'end') + textbox2.insert('end', str(message)) + textbox2.insert('end', " millas") + +def convert_ml_to_km(): + ml = textbox1.get() + ml = int(ml) + message = round(ml * 1.6093, 3) + textbox2.delete(0, 'end') + textbox2.insert('end', str(message)) + textbox2.insert('end', " kilometros") + +window = Tk() +window.title("Distancia") +window.geometry("260x200") + +label1 = Label(text="Ingresa el valor a convertir:") +label1.place(x=30, y=20) + +textbox1 = Entry() +textbox1.place(x=30, y=50, width=200, height=25) +textbox1["justify"] = "center" +textbox1.focus() + +convert1 = Button(text="Convertir milas a kilometros", command=convert_ml_to_km) +convert1.place(x=30, y=80, width=200, height=25) + +convert2 = Button(text="Convertir kilometros a milas", command=convert_km_to_ml) +convert2.place(x=30, y=110, width=200, height=25) + +textbox2 = Entry() +textbox2.place(x=30, y=140, width=200, height=25) +textbox2["justify"] = "center" + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_06.py b/tkgui/sols_124-132/sol_06.py new file mode 100644 index 0000000..7c9561d --- /dev/null +++ b/tkgui/sols_124-132/sol_06.py @@ -0,0 +1,37 @@ +from tkinter import * + +def add_number(): + num = num_box.get() + if num.isdigit(): + num_list.insert('end', num) + num_box.delete(0, 'end') + num_box.focus() + else: + num_box.delete(0, 'end') + num_box.focus() + +def clear_list(): + num_list.delete(0, 'end') + num_box.focus() + +window = Tk() +window.title("Lista de Números") +window.geometry("450x200") + +label1 = Label(text="Ingresa un número:") +label1.place(x=20, y=20, width=150, height=25) + +num_box = Entry() +num_box.place(x=170, y=20, width=100, height=25) +num_box.focus() + +button1 = Button(text="Agregar", command=add_number) +button1.place(x=300, y=20, width=100, height=25) + +num_list = Listbox() +num_list.place(x=170, y=50, width=100, height=100) + +button2 = Button(text="Limpiar", command=clear_list) +button2.place(x=300, y=50, width=100, height=25) + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_07.py b/tkgui/sols_124-132/sol_07.py new file mode 100644 index 0000000..c794f2d --- /dev/null +++ b/tkgui/sols_124-132/sol_07.py @@ -0,0 +1,51 @@ +from tkinter import * +import csv + +def add_number(): + num = num_box.get() + if num.isdigit(): + num_list.insert('end', num) + num_box.delete(0, 'end') + num_box.focus() + else: + num_box.delete(0, 'end') + num_box.focus() + +def clear_list(): + num_list.delete(0, 'end') + num_box.focus() + +def save_list(): + with open('/tmp/nums.csv', '+a') as file: + tmp_list = num_list.get(0, 'end') + item = 0 + for _ in tmp_list: + newrecord = tmp_list[item]+'\n' + file.write(str(newrecord)) + item += 1 + file.close() + +window = Tk() +window.title("Lista de Números") +window.geometry("400x200") + +label1 = Label(text="Ingresa un nro.") +label1.place(x=20, y=20, width=100, height=25) + +num_box = Entry() +num_box.place(x=120, y=20, width=100, height=25) +num_box.focus() + +button1 = Button(text="Agregar", command=add_number) +button1.place(x=250, y=20, width=100, height=25) + +num_list = Listbox() +num_list.place(x=120, y=50, width=100, height=100) + +button2 = Button(text="Limpiar", command=clear_list) +button2.place(x=250, y=50, width=100, height=25) + +button3 = Button(text="Guardar", command=save_list) +button3.place(x=250, y=80, width=100, height=25) + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_08.py b/tkgui/sols_124-132/sol_08.py new file mode 100644 index 0000000..7c920ab --- /dev/null +++ b/tkgui/sols_124-132/sol_08.py @@ -0,0 +1,41 @@ +from tkinter import * +import csv + +def create_new(): + file = open("/tmp/ages.csv", "w") + file.close() + +def save_list(): + with open("/tmp/ages.csv", "a") as file: + name = name_box.get() + age = age_box.get() + newrecord = name + ", " + age + "\n" + file.write(str(newrecord)) + name_box.delete(0, 'end') + age_box.delete(0, 'end') + name_box.focus() + +window = Tk() +window.title("Lista Personas") +window.geometry("400x100") + +label1 = Label(text="Ingresa un nombre:") +label1.place(x=20, y=20, width=100, height=25) + +name_box = Entry() +name_box.place(x=120, y=20, width=100, height=25) +name_box["justify"] = "left" +name_box.focus() + +label2 = Label(text="Ingresa la edad:") +label2.place(x=20, y=50, width=100, height=25) + +age_box = Entry() +age_box.place(x=120, y=50, width=100, height=25) + +button1 = Button(text="Crear nuevo CSV", command=create_new) +button1.place(x=250, y=20, width=100, height=25) +button2 = Button(text="Agregar al CSV", command=create_new) +button2.place(x=250, y=50, width=100, height=25) + +window.mainloop() diff --git a/tkgui/sols_124-132/sol_09.py b/tkgui/sols_124-132/sol_09.py new file mode 100644 index 0000000..246b968 --- /dev/null +++ b/tkgui/sols_124-132/sol_09.py @@ -0,0 +1,55 @@ +from tkinter import * +import csv + +def save_list(): + file = open('/tmp/ages.csv', 'a') + name = name_box.get() + age = age_box.get() + newrecord = name + ', ' + age + '\n' + file.write(str(newrecord)) + file.close() + +def read_list(): + name_list.delete(0, 'end') + file = list(csv.reader(open('/tmp/ages.csv'))) + tmp = [] + for row in file: + tmp.append(row) + x = 0 + for i in tmp: + data = tmp[x] + name_list.insert('end', data) + x += 1 + +window = Tk() +window.title("Lista Personas") +window.geometry("400x200") + +label1 = Label(text="Nombre") +label1.place(x=20, y=20, width=100, height=25) + +name_box = Entry() +name_box.place(x=120, y=20, width=100, height=25) +name_box["justify"] = "left" +name_box.focus() + +label2 = Label(text="Edad") +label2.place(x=20, y=50, width=100, height=25) + +age_box = Entry() +age_box.place(x=120, y=50, width=100, height=25) +age_box["justify"] = "left" + +button1 = Button(text="Agregar a archivo", command=save_list) +button1.place(x=250, y=20, width=100, height=25) + +button2 = Button(text="Leer archivo", command=read_list) +button2.place(x=250, y=50, width=100, height=25) + +label3 = Label(text="Registros:") +label3.place(x=20, y=80, width=100, height=25) + +name_list = Listbox() +name_list.place(x=120, y=80, width=230, height=100) + +window.mainloop() diff --git a/tkgui/tk01.py b/tkgui/tk01.py new file mode 100644 index 0000000..076117c --- /dev/null +++ b/tkgui/tk01.py @@ -0,0 +1,46 @@ +import tkinter as tk + +def tk_01(): + """Create a window that will ask the user to enter their name. When they + click on a btn_acept it should display the message 'Hello' and their + name and change the background colour and font colour of the message + box.""" + def click(): + name = txt_in_box.get() + if name == "": + reset() + else: + txt_out_box["fg"] = "black" + txt_out_box["bg"] = "magenta" + txt_out_box["text"] = f"Hola {name}" + txt_in_box.delete(0, 'end') + txt_in_box.focus() + + def reset(): + txt_out_box["fg"] = "red" + txt_out_box["bg"] = "cyan" + txt_out_box["text"] = "" + + window = tk.Tk() + window.title("Saludo TK") + window.geometry("390x200") + + lbl_info = tk.Label(text="Ingresa tu nombre: ") + lbl_info.place(x=40, y=25, width=150, height=35) + + txt_in_box = tk.Entry() + txt_in_box["justify"] = "center" + txt_in_box.place(x=180, y=25, width=150, height=35) + txt_in_box.focus() + + txt_out_box = tk.Message(text="") + txt_out_box["fg"] = "red" + txt_out_box["bg"] = "cyan" + txt_out_box["justify"] = "center" + txt_out_box["width"] = 250 + txt_out_box["font"] = "TkHeadingFont" + txt_out_box.place(x=40, y=120, width=300, height=50) + + btn_acept = tk.Button(text="ACEPTAR", command=click) + btn_acept.place(x=40, y=75, width=300, height=35) + window.mainloop() diff --git a/tkgui/tk02.py b/tkgui/tk02.py new file mode 100644 index 0000000..0aa3fd4 --- /dev/null +++ b/tkgui/tk02.py @@ -0,0 +1,31 @@ +import tkinter as tk +from random import randint + +def tk_02(): + """Write a program that can be used instead of rolling a six-sided die in a + board game. When the user clicks a button it should display a random + whole number between 1 to 6 (inclusive).""" + def click(): + num = randint(1,6) + txt_out_box["fg"] = "black" + txt_out_box["bg"] = "magenta" + txt_out_box["text"] = f"{num}" + + window = tk.Tk() + window.title("Dado TK") + window.geometry("380x450") + + lbl_info = tk.Label(text="Dado", font="Verdana 50") + lbl_info.place(x=40, y=25, width=300, height=75) + + txt_out_box = tk.Message(text="", font="Verdana 120") + txt_out_box["fg"] = "red" + txt_out_box["bg"] = "cyan" + txt_out_box["justify"] = "center" + txt_out_box["width"] = 250 + txt_out_box.place(x=40, y=120, width=300, height=300) + + btn_launch = tk.Button(text="LANZAR DADO", command=click) + btn_launch.place(x=40, y=100, width=300, height=35) + btn_launch.focus() + window.mainloop() diff --git a/tkgui/tk03.py b/tkgui/tk03.py new file mode 100644 index 0000000..e35e49a --- /dev/null +++ b/tkgui/tk03.py @@ -0,0 +1,50 @@ +import tkinter as tk + +def tk_03(): + """Create a program that will ask the user to enter a number in a box. When + they click on a button it will add that number to a total and display it + in another box. This can be repeated as many times as they want and keep + adding to the total. There should be another button that resets the total + back to 0 and empties the original text box, ready for them to start again.""" + def click(): + if txt_in_box.get() != '': + num = int(txt_in_box.get()) + if txt_out_box["text"] == "0": + txt_out_box["text"] = f"{num}" + else: + res = int(txt_out_box["text"]) + num + txt_out_box["text"] = f"{res}" + txt_in_box.delete(0, 'end') + txt_in_box.delete(0, 'end') + txt_in_box.focus() + + def reset(): + txt_out_box["text"] = "0" + txt_in_box.delete(0, 'end') + txt_in_box.focus() + + window = tk.Tk() + window.title("Suma TK") + window.geometry("390x230") + + lbl_info = tk.Label(text="Ingresa un número: ") + lbl_info.place(x=40, y=25, width=150, height=35) + + txt_in_box = tk.Entry() + txt_in_box["justify"] = "center" + txt_in_box.place(x=180, y=25, width=150, height=35) + txt_in_box.focus() + + txt_out_box = tk.Message(text="0") + txt_out_box["fg"] = "black" + txt_out_box["bg"] = "magenta" + txt_out_box["justify"] = "center" + txt_out_box["width"] = 250 + txt_out_box["font"] = "TkHeadingFont" + txt_out_box.place(x=40, y=115, width=300, height=50) + + btn_acept = tk.Button(text="AGREGAR AL TOTAL", command=click) + btn_acept.place(x=40, y=75, width=300, height=35) + btn_reset = tk.Button(text="BORRAR TOTAL", command=reset) + btn_reset.place(x=40, y=170, width=300, height=35) + window.mainloop() diff --git a/tkgui/tk04.py b/tkgui/tk04.py new file mode 100644 index 0000000..8f06874 --- /dev/null +++ b/tkgui/tk04.py @@ -0,0 +1,38 @@ +import tkinter as tk + +def tk_04(): + """Create a window that will ask the user to enter a name in a text box. + When they click on a button it will add it to the end of the list that is + displayed on the screen. Create another button which will clear the list.""" + def click(): + name = txt_in_box.get() + if name != "": + lst_names.insert('end', name) + txt_in_box.delete(0, 'end') + txt_in_box.focus() + + def reset(): + lst_names.delete(0, 'end') + txt_in_box.focus() + + window = tk.Tk() + window.title("Lista TK") + window.geometry("390x350") + + lbl_info = tk.Label(text="Ingresa un nombre: ") + lbl_info.place(x=40, y=25, width=150, height=35) + + txt_in_box = tk.Entry() + txt_in_box["justify"] = "center" + txt_in_box.place(x=180, y=25, width=150, height=35) + txt_in_box.focus() + + btn_add = tk.Button(text="Agregar", command=click) + btn_add.place(x=40, y=75, width=150, height=35) + btn_reset = tk.Button(text="Vaciar", command=reset) + btn_reset.place(x=190, y=75, width=150, height=35) + + lst_names = tk.Listbox() + lst_names.place(x=40, y=120, width=300, height=200) + + window.mainloop() diff --git a/tkgui/tk05.py b/tkgui/tk05.py new file mode 100644 index 0000000..18e9726 --- /dev/null +++ b/tkgui/tk05.py @@ -0,0 +1,71 @@ +import tkinter as tk + +def tk_05(): + """[ 1 kilometre = 0.6214 miles] & [ 1 mile = 1.6093 kilometres] + Using these figures, make a program that will allow the user to convert + between miles and kilometres.""" + def conv_km_to_ml(): + kms = txt_in_box.get() + if kms != "": + mls = int(kms) * 0.6214 + txt_conv["text"] = round(mls, 3) + #txt_in_box.delete(0, 'end') + lbl_in["text"] = "Kilometros" + lbl_out["text"] = "Millas" + txt_in_box.focus() + + def conv_ml_to_km(): + mls = txt_in_box.get() + if mls != "": + mls = int(mls) * 1.6093 + txt_conv["text"] = round(mls, 3) + lbl_in["text"] = "Millas" + lbl_out["text"] = "Kilometros" + txt_in_box.focus() + + def reset(): + txt_in_box.delete(0, 'end') + lbl_in["text"] = "" + lbl_out["text"] = "" + txt_conv["text"] = "" + txt_in_box.focus() + + window = tk.Tk() + window.title("Conversor Distancias TK") + window.geometry("320x230") + + lbl_in = tk.Label(text="") + lbl_in["justify"] = "left" + lbl_in.place(x=190, y=25, width=100, height=35) + + txt_in_box = tk.Entry() + txt_in_box["justify"] = "center" + txt_in_box.place(x=40, y=25, width=150, height=35) + txt_in_box.focus() + + btn_conv1 = tk.Button(text="Convertir a millas", command=conv_km_to_ml) + btn_conv1["fg"] = "black" + btn_conv1["bg"] = "cyan" + btn_conv1.place(x=40, y=120, width=150, height=35) + + btn_conv2 = tk.Button(text="Convertir a kilometros", command=conv_ml_to_km) + btn_conv2["fg"] = "black" + btn_conv2["bg"] = "cyan" + btn_conv2.place(x=40, y=160, width=150, height=35) + + btn_clr = tk.Button(text="Limpiar", command=reset) + btn_clr["fg"] = "black" + btn_clr["bg"] = "cyan" + btn_clr.place(x=200, y=120, width=80, height=75) + + lbl_out = tk.Label(text="") + lbl_out["justify"] = "left" + lbl_out.place(x=190, y=75, width=100, height=35) + + txt_conv = tk.Message(text=0) + txt_conv["bg"] = "magenta" + txt_conv["fg"] = "black" + txt_conv["width"] = 100 + txt_conv.place(x=40, y=75, width=150, height=35) + + window.mainloop() diff --git a/tkgui/tk06.py b/tkgui/tk06.py new file mode 100644 index 0000000..6f06310 --- /dev/null +++ b/tkgui/tk06.py @@ -0,0 +1,45 @@ +import tkinter as tk + +def tk_06(): + """Create a window that will ask the user to enter a number in a text box. + When they click on a button it will use the code variable.isdigit() to + check to see if it is a whole number. If it is a whole number, add it to + a list box, otherwise clear the entry box. Add another button that will + clear the list.""" + def add_num(): + name = txt_in_box.get() + if name.isdigit(): + lst_nums.insert('end', name) + txt_in_box.delete(0, 'end') + txt_in_box.focus() + else: + txt_in_box.delete(0, 'end') + txt_in_box.focus() + + def reset(): + txt_in_box.delete(0, 'end') + lst_nums.delete(0, 'end') + txt_in_box.focus() + + window = tk.Tk() + window.title("Lista de números TK") + window.geometry("390x350") + + lbl_info = tk.Label(text="Ingresa un número: ") + lbl_info.place(x=40, y=25, width=150, height=35) + + txt_in_box = tk.Entry() + txt_in_box["justify"] = "center" + txt_in_box.place(x=180, y=25, width=150, height=35) + txt_in_box.focus() + + btn_add = tk.Button(text="Agregar", command=add_num) + btn_add.place(x=40, y=75, width=150, height=35) + btn_reset = tk.Button(text="Vaciar", command=reset) + btn_reset.place(x=190, y=75, width=150, height=35) + + lst_nums = tk.Listbox() + lst_nums["justify"] = "center" + lst_nums.place(x=40, y=120, width=300, height=200) + + window.mainloop() diff --git a/tkgui/tk07.py b/tkgui/tk07.py new file mode 100644 index 0000000..9128e6c --- /dev/null +++ b/tkgui/tk07.py @@ -0,0 +1,68 @@ +import tkinter as tk +from os import getcwd as pwd +import csv + +def tk_07(): + """Alter program 129 to add a third button that will save the list to a .csv + file. The code 'tmp_list = num_list.get(0,"end")' can be used to save the + contents of a list box as a tuple called tmp_list.""" + file_path = f"{pwd()}/tkgui/files/nums.csv" + def add_num(): + num = txt_in_box.get() + if num.isdigit(): + lst_nums.insert('end', num) + txt_in_box.delete(0, 'end') + txt_in_box.focus() + else: + txt_in_box.delete(0, 'end') + txt_in_box.focus() + + def reset(): + txt_in_box.delete(0, 'end') + lst_nums.delete(0, 'end') + txt_in_box.focus() + + def save_csv(): + tmp_lst = lst_nums.get(0, 'end') + with open(file_path, 'a+') as file: + item = 0 + for _ in tmp_lst: + new_num = tmp_lst[item]+'\n' + file.write(str(new_num)) + item += 1 + path_csv = f"Ruta: {file_path}" + lbl_file["state"] = "normal" + lbl_file.delete(0, 'end') + lbl_file.insert(0, path_csv) + lbl_file["state"] = "readonly" + + window = tk.Tk() + window.title("Lista de números TK") + window.geometry("600x240") + + lbl_info = tk.Label(text="Ingresa un número: ") + lbl_info.place(x=40, y=25, width=150, height=35) + + txt_in_box = tk.Entry() + txt_in_box["justify"] = "center" + txt_in_box.place(x=180, y=25, width=150, height=35) + txt_in_box.focus() + + btn_add = tk.Button(text="Agregar", command=add_num) + btn_add.place(x=40, y=75, width=300, height=50) + btn_reset = tk.Button(text="Vaciar", command=reset) + btn_reset.place(x=40, y=130, width=140, height=50) + btn_save = tk.Button(text="Guardar", command=save_csv) + btn_save.place(x=200, y=130, width=140, height=50) + + lst_nums = tk.Listbox() + lst_nums["justify"] = "center" + lst_nums.place(x=350, y=25, width=220, height=160) + + lbl_file = tk.Entry() + lbl_file["state"] = "readonly" + lbl_file["justify"] = "center" + lbl_file["width"] = 580 + lbl_file.place(x=10, y=190, width=580, height=35) + + window.mainloop() diff --git a/tkgui/tk08.py b/tkgui/tk08.py new file mode 100644 index 0000000..2264c56 --- /dev/null +++ b/tkgui/tk08.py @@ -0,0 +1,64 @@ +import tkinter as tk +from os import getcwd as pwd +import csv + +def tk_08(): + """Create a program that will allow the user to create a new .csv file. It + should ask them to enter the name and age of a person and then allow them + to add this to the end of the file they have just created.""" + file_path = f"{pwd()}/tkgui/files/names.csv" + def add_person(): + name = txt_in_name.get() + age = txt_in_age.get() + if age.isdigit(): + new_person = [name, age] + with open(file_path, '+a') as file: + writer = csv.writer(file) + writer.writerow(new_person) + clear_entries() + else: + clear_entries() + + def clear_entries(): + txt_in_name.delete(0, 'end') + txt_in_age.delete(0, 'end') + txt_in_name.focus() + + def create_csv(): + open(file_path, '+w').close() + path_csv = f"Ruta: {file_path}" + lbl_file["state"] = "normal" + lbl_file.delete(0, 'end') + lbl_file.insert(0, path_csv) + lbl_file["state"] = "readonly" + + window = tk.Tk() + window.title("Personas TK") + window.geometry("600x160") + + lbl_name = tk.Label(text="Nombre") + lbl_name.place(x=40, y=25, width=100, height=35) + lbl_age = tk.Label(text= "Edad") + lbl_age.place(x=40, y=60, width=100, height=35) + + txt_in_name = tk.Entry() + txt_in_name["justify"] = "center" + txt_in_name.place(x=140, y=25, width=150, height=35) + txt_in_name.focus() + + txt_in_age = tk.Entry() + txt_in_age["justify"] = "center" + txt_in_age.place(x=140, y=60, width=150, height=35) + + btn_add = tk.Button(text="Agregar a archivo", command=add_person) + btn_add.place(x=300, y=25, width=250, height=35) + btn_create = tk.Button(text="Crear archivo", command=create_csv) + btn_create.place(x=300, y=60, width=250, height=35) + + lbl_file = tk.Entry() + lbl_file["state"] = "readonly" + lbl_file["justify"] = "center" + lbl_file["width"] = 580 + lbl_file.place(x=10, y=110, width=580, height=35) + + window.mainloop() diff --git a/tkgui/tk09.py b/tkgui/tk09.py new file mode 100644 index 0000000..4858bcb --- /dev/null +++ b/tkgui/tk09.py @@ -0,0 +1,78 @@ +import tkinter as tk +from os import getcwd as pwd +import csv + +def tk_09(): + """Using the .csv file you created for the last challenge, create a program + that will allow people to add names and ages to the list and create a + button that will display the contents of the .csv file by importing it + to a list box.""" + file_path = f"{pwd()}/tkgui/files/names2.csv" + def add_person(): + name = txt_in_name.get() + age = txt_in_age.get() + if age.isdigit(): + new_person = [name, age] + with open(file_path, '+a') as file: + writer = csv.writer(file) + writer.writerow(new_person) + clear_entries() + updt_lbl_file() + else: + clear_entries() + + def clear_entries(): + txt_in_name.delete(0, 'end') + txt_in_age.delete(0, 'end') + txt_in_name.focus() + + def read_csv(): + clear_entries() + box_out.delete(0, 'end') + with open(file_path, 'r') as file: + reader = csv.reader(file) + contnt = list(reader) + for line in contnt: + box_out.insert('end',(line[0].ljust(100)+" "+line[1].ljust(10)).rjust(130)) + updt_lbl_file() + + def updt_lbl_file(): + path_csv = f"Ruta: {file_path}" + lbl_file["state"] = "normal" + lbl_file.delete(0, 'end') + lbl_file.insert(0, path_csv) + lbl_file["state"] = "readonly" + + window = tk.Tk() + window.title("Personas TK") + window.geometry("600x330") + + lbl_name = tk.Label(text="Nombre") + lbl_name.place(x=40, y=25, width=100, height=35) + lbl_age = tk.Label(text= "Edad") + lbl_age.place(x=40, y=60, width=100, height=35) + + txt_in_name = tk.Entry() + txt_in_name["justify"] = "center" + txt_in_name.place(x=140, y=25, width=150, height=35) + txt_in_name.focus() + + txt_in_age = tk.Entry() + txt_in_age["justify"] = "center" + txt_in_age.place(x=140, y=60, width=150, height=35) + + btn_add = tk.Button(text="Agregar a archivo", command=add_person) + btn_add.place(x=300, y=25, width=250, height=35) + btn_save = tk.Button(text="Importar archivo", command=read_csv) + btn_save.place(x=300, y=60, width=250, height=35) + + box_out = tk.Listbox() + box_out.place(x=40, y=110, width=510, height=160) + + lbl_file = tk.Entry() + lbl_file["state"] = "readonly" + lbl_file["justify"] = "center" + lbl_file["width"] = 580 + lbl_file.place(x=10, y=280, width=580, height=35) + + window.mainloop() diff --git a/tkgui/tkgui.py b/tkgui/tkgui.py new file mode 100644 index 0000000..312e926 --- /dev/null +++ b/tkgui/tkgui.py @@ -0,0 +1,59 @@ +from . import tk01 as ex01 +from . import tk02 as ex02 +from . import tk03 as ex03 +from . import tk04 as ex04 +from . import tk05 as ex05 +from . import tk06 as ex06 +from . import tk07 as ex07 +from . import tk08 as ex08 +from . import tk09 as ex09 +from common.common import ( + user_input, + print_run_func, + opcs_default, + clear +) + +tab = ' ' + +def challenges01(): + select_ok = False + while not select_ok: + clear() + print(tab, '1)', ex01.tk_01.__doc__) + print(tab, '2)', ex02.tk_02.__doc__) + print(tab, '3)', ex03.tk_03.__doc__) + print(tab, '4)', ex04.tk_04.__doc__) + print(tab, '5)', ex05.tk_05.__doc__) + print(tab, '6)', ex06.tk_06.__doc__) + print(tab, '7)', ex07.tk_07.__doc__) + print(tab, '8)', ex08.tk_08.__doc__) + print(tab, '9)', ex09.tk_09.__doc__) + opcs_default(1) + selection = user_input(9) + match selection: + case 1: + print_run_func(ex01.tk_01) + case 2: + print_run_func(ex02.tk_02) + case 3: + print_run_func(ex03.tk_03) + case 4: + print_run_func(ex04.tk_04) + case 5: + print_run_func(ex05.tk_05) + case 6: + print_run_func(ex06.tk_06) + case 7: + print_run_func(ex07.tk_07) + case 8: + print_run_func(ex08.tk_08) + case 9: + print_run_func(ex09.tk_09) + case 'v': + return + case 's': + select_ok = True + exit(0) + case _: + continue