Created
November 25, 2019 11:19
-
-
Save aarroyoc/b01d7978e920b7b375dbb7bdf4bbf1ea to your computer and use it in GitHub Desktop.
Termometro Tkinter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tkinter import * | |
| from tkinter import ttk | |
| class Termometro(Tk): | |
| def __init__(self): | |
| Tk.__init__(self) | |
| self.geometry("210x350") | |
| self.title("Termometro") | |
| self.configure(bg="SystemButtonFace") | |
| self.resizable(0,0) | |
| self.old_temp = "0.0" | |
| self.temp_string = StringVar(value="0.0") | |
| self.temp_string.trace("w", self.validate_temp) | |
| self.temp_unit = StringVar(value="C") | |
| self.style = ttk.Style() | |
| self.style.configure("new.TRadiobutton", background="SystemButtonFace") | |
| self.style.configure("new.TLabel", background="SystemButtonFace") | |
| self.make_layout() | |
| def make_layout(self): | |
| self.temp_input = ttk.Entry(self, textvariable=self.temp_string).place(x=10, y=10) | |
| self.lb_unidad = ttk.Label(self, text="Grados:", style="new.TLabel").place(x=10, y=40) | |
| self.rb_c = ttk.Radiobutton(self, text="Celsius", variable=self.temp_unit, value="C", style="new.TRadiobutton", command=self.select).place(x=20, y=60) | |
| self.rb_f = ttk.Radiobutton(self, text="Fahrenheit", variable=self.temp_unit, value="F", style="new.TRadiobutton", command=self.select).place(x=20, y=80) | |
| def select(self): | |
| try: | |
| degree = float(self.temp_string.get()) | |
| unit = self.temp_unit.get() | |
| if unit == "C": | |
| out = (degree - 32)*5/9 | |
| else: | |
| out = degree*9/5 + 32 | |
| self.temp_string.set(out) | |
| self.old_tmp = out | |
| except: | |
| pass | |
| def validate_temp(self, *args): | |
| new_tmp = self.temp_string.get() | |
| try: | |
| float(new_tmp) | |
| self.old_temp = new_tmp | |
| except: | |
| if new_tmp: | |
| self.temp_string.set(self.old_temp) | |
| app = Termometro() | |
| app.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment