Advertisement
JPablos

Cambiar base a Binario, Octal y Hexadecimal. Simple. Python

Jun 3rd, 2023
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | Science | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jun  2 06:33:22 2023
  4. Este código permite cambiar un número decimal ingresado por el usuario
  5. a otras bases, como a Binario, Octal o Hexadecimal.
  6.  
  7. Utiliza tkinter para crear una interfaz gráfica.
  8.  
  9. @alpfa
  10. """
  11.  
  12. import tkinter as tk
  13. from tkinter import ttk
  14.  
  15.  
  16. def convert():
  17.     """
  18.    Código que permite el cambio de base 10 a Binario, Octal y Hexadecimal.
  19.  
  20.    Returns
  21.    -------
  22.    La correspondencia en las bases citadas.
  23.  
  24.    """
  25.     decimal = int(decimal_entry.get())
  26.     binary = bin(decimal)[2:]
  27.     hexadecimal = hex(decimal)[2:]
  28.     octal = oct(decimal)[2:]
  29.  
  30.     result_text.delete("1.0", tk.END)
  31.     result_text.insert(tk.END, f"Decimal ingresado: {decimal}\n\n")
  32.     result_text.insert(tk.END, "Conversión a otras bases:\n\n")
  33.     result_text.insert(tk.END, f"Binario (0:b):     {binary}\n")
  34.     result_text.insert(tk.END, f"Octal (0:o):       {octal}\n")
  35.     result_text.insert(tk.END, f"Hexadecimal (0:x): {hexadecimal}")
  36.  
  37.  
  38. root = tk.Tk()
  39. root.title("Cambio de Base a Binario, Octal y Hexadecimal")
  40.  
  41. mainframe = ttk.Frame(root, padding="3 3 12 12")
  42. mainframe.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S))
  43. root.columnconfigure(0, weight=1)
  44. root.rowconfigure(0, weight=1)
  45.  
  46. decimal_label = tk.Label(
  47.     mainframe, text="Cambiar base, ingrese un número Decimal y presione:   ")
  48. decimal_label.grid(column=1, row=2, sticky=tk.W)
  49.  
  50. decimal_entry = tk.Entry(mainframe)
  51. decimal_entry.grid(column=2, row=1, sticky=tk.W)
  52.  
  53. convert_button = tk.Button(mainframe, text="Convertir", command=convert)
  54. convert_button.grid(column=2, row=2, sticky=tk.W)
  55.  
  56. result_text = tk.Text(mainframe, height=8, width=50)
  57. result_text.grid(column=2, row=3, rowspan=4, sticky=(tk.W, tk.E))
  58. result_text.tag_configure("title", font=("Courier New", 12, "bold"))
  59.  
  60. result_text.insert(tk.END, "Resultado:\n", "title")
  61.  
  62. root.mainloop()
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement