teslariu

tkint

Feb 12th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # place
  5. import tkinter as tk
  6. from tkinter import ttk
  7. from tkinter import messagebox
  8.  
  9. def saludar():
  10.     nombre = caja.get()
  11.     if nombre:
  12.         print(f"Hola {nombre}")         # lo muestra por consola
  13.         saludo.set(f"Hola {nombre}")  # lo muestra en entorno gráfico
  14.     else:
  15.         print(f"Hola desconocido")
  16.     caja.delete(0,tk.END)
  17.  
  18.  
  19. def seleccion_lista():
  20.     seleccion = lista.get(lista.curselection())
  21.     print(f"Selección: {seleccion}")
  22.  
  23. def seleccion_combo():
  24.     seleccion = combo.get()
  25.     print(f"Selección: {seleccion}")
  26.  
  27.  
  28. root = tk.Tk()
  29. root.title("Posicionamiento place")
  30. root.config(width=400, height=800, bg="sky blue")
  31.  
  32. saludo = tk.StringVar()  # creo la variable gráfica para mostrar el saludo
  33. saludo.set("desconocido")  #la inicializo
  34.  
  35.  
  36. # boton con caja de texto y etiqueta
  37. caja = ttk.Entry()   # caja para ingresar el nombre
  38. caja.place(x=10, y=40, width=100, height=25)
  39. label = tk.Label(text="Nombre", bg="sky blue", font=["arial",12,"bold","italic"])
  40. label.place(x=130, y=40)
  41. boton = ttk.Button(text="Saludar", command=saludar)
  42. boton.place(x=90, y=80)
  43.  
  44. # caja para mostrar el saludo
  45. caja_saludo = ttk.Entry(
  46.             textvariable=saludo,
  47.             state=tk.DISABLED
  48.             )  
  49.            
  50. caja_saludo.place(x=10, y=120, width=180, height=25)
  51.  
  52. # lista desplegada (listbox)
  53. lista = tk.Listbox()
  54. lista.insert(0, "Python","GO","JAVA","Erlang","C++")
  55. lista.place(x=10, y=170)
  56. boton = ttk.Button(text="Seleccionar", command=seleccion_lista)
  57. boton.place(x=10, y=350)
  58.  
  59. # lista desplegable (combobox)
  60. combo = ttk.Combobox(state="readonly", values=[1,2,3,4,5,6,7,8])
  61. combo.place(x=10, y=400)
  62. boton = ttk.Button(text="Seleccionar", command=seleccion_combo)
  63. boton.place(x=10, y=450)
  64.  
  65. # casilla de verificacion
  66. estado = tk.BooleanVar()
  67. estado.set("False")
  68. casilla = tk.Checkbutton(text="Acepta las condiciones", variable=estado, bg="sky blue")
  69. casilla.place(x=10, y=500)
  70.  
  71. # barra de progreso
  72. barra = ttk.Progressbar(maximum=100)
  73. barra.place(x=10, y=550, width=200)
  74. # "coloreo" la barra
  75. barra.step(25)
  76. # le doy movimiento
  77. barra.start(10)
  78.  
  79. barra = ttk.Progressbar(orient=tk.VERTICAL)
  80. barra.place(x=350, y=550, height=200)
  81. barra.step(90)
  82. barra.start()
  83.  
  84. # imagen dentro de una etiqueta
  85. imagen = tk.PhotoImage(file="camion.png")
  86. label = ttk.Label(image=imagen)
  87. label.place(relx=0.5, rely=0.5, relwidth=0.5, relheight=0.5)
  88.  
  89. # cuadros de dialogo
  90.  
  91. # siempre retorna la cadena "ok"
  92. messagebox.showinfo(title="INFO", message="Lo estoy observando...")
  93. messagebox.showwarning(title="ADVERTENCIA", message="Ojo...")
  94. messagebox.showerror(title="ERROR", message="Mejor no haga nada...")
  95.  
  96. # siempre retorna True of False"
  97. messagebox.askokcancel(title="Pregunta", message="¿Desea salir?")
  98. messagebox.askyesno(title="Pregunta", message="Decídase")
  99. messagebox.askretrycancel(title="Pregunta", message="¿Desea reintentarlo?")
  100.  
  101.  
  102.  
  103.  
  104. root.mainloop()
  105.  
Add Comment
Please, Sign In to add comment