Tk
themed widgets. All the widgets that tkinter has also available in tkinter.ttk.tkinter.ttk
button is little bit creepy because it doesn’t support direct implementation. To add styling in a ttk.Button
we have to first create a object of style class which is available in tkinter.ttk
.ttk.Button
we cannot directly pass the value in the options. Firstly, we have to create a Style object which can be created as follows:from tkinter.ttk import * root = Tk() style = Style() # This will be adding style, and # W.Tbutton (TButton is used for ttk.Button). style.configure( 'W.TButton' , font = foreground = 'red' ) # Style will be reflected only on # style only on this Button. btn1 = Button(root, text = 'Quit !' , command = root.destroy) btn2 = Button(root, text = 'Click me !' , command = None ) btn2.grid(row = 1 , column = 3 , pady = 10 , padx = 100 ) root.mainloop() |
from tkinter.ttk import * root = Tk() # Will add style to every available button # to every button widget. ( 'calibri' , 10 , 'bold' , 'underline' ), # button 1 style = 'TButton' , btn2 = Button(root, text = 'Click me !' , command = None ) btn2.grid(row = 1 , column = 3 , pady = 10 , padx = 100 ) root.mainloop() |
from tkinter.ttk import * root = Tk() style.configure( 'TButton' , font = borderwidth = '4' ) # Changes will be reflected style. map ( 'TButton' , foreground = [( 'active' , '! disabled' , 'green' )], btn1 = Button(root, text = 'Quit !' , command = root.destroy) btn2 = Button(root, text = 'Click me !' , command = None ) btn2.grid(row = 1 , column = 3 , pady = 10 , padx = 100 ) root.mainloop() |
#!/usr/bin/env python |
# -*- coding: utf-8 -*- |
try: |
# Python 2.x |
from Tkinter import* |
from ttk import* |
from tkMessageBox import* |
exceptImportError: |
# Python 3.x |
from tkinter import* |
from tkinter.ttk import* |
from tkinter.messagebox import* |
# Ver http://www.python-course.eu/tkinter_dialogs.php |
dolar =5080 |
defcotizar(*args): |
try: |
guaranies.set(int(dolares.get())*dolar) |
exceptValueError: |
showerror(title='Error', message='Sólo números arábigos, por favor.') |
dolares.set('') |
ventana = Tk() |
ventana.title('Dólares a Guaraníes') |
marco = Frame(ventana, padding='10 10 10 10') |
marco.grid(column=0, row=0, sticky=(N, W, E, S)) |
dolares = StringVar() |
guaranies = StringVar() |
entrada = Entry(marco, width=7, textvariable=dolares) |
entrada.grid(column=2, row=1, sticky=(W, E)) |
Label(marco, textvariable=guaranies).grid(column=2, row=2, sticky=(W, E)) |
Button(marco, text='Cotizar', command=cotizar).grid(column=3, row=3, sticky=W) |
Label(marco, text='dólares').grid(column=3, row=1, sticky=W) |
Label(marco, text='equivale a').grid(column=1, row=2, sticky=E) |
Label(marco, text='guaraníes').grid(column=3, row=2, sticky=W) |
# 1 2 3 |
# ------------------------------------------------------------- |
# 1 | | [entrada] | dólares | |
# ------------------------------------------------------------- |
# 2 | equivale a | {guaranies} | guaraníes | |
# ------------------------------------------------------------- |
# 3 | | | < Cotizar > | |
# ------------------------------------------------------------- |
for hijo in marco.winfo_children(): hijo.grid_configure(padx=5, pady=5) |
entrada.focus() |
ventana.bind('<Return>', cotizar) |
ventana.bind('<Escape>', lambdax: ventana.destroy()) |
ventana.mainloop() |
#!/usr/bin/env python |
# -*- coding: utf-8 -*- |
from Tkinter import* |
ventana = Tk() |
defboton(evento): |
print'Puntero en', str(evento.x), str(evento.y) |
marco = Frame(ventana, width=640, height=480) |
marco.bind('<Button-1>', boton) |
marco.pack() |
ventana.mainloop() |
#!/usr/bin/env python |
# -*- coding: utf-8 -*- |
try: |
# Python 2.x |
from Tkinter import* |
from ttk import* |
exceptImportError: |
# Python 3.x |
from tkinter import* |
from tkinter.ttk import* |
import random |
premio = random.randint(1, 3) |
defapostar(numero): |
if numero premio: |
etiqueta.configure(text='¡Ganaste!') |
else: |
etiqueta.configure(text='Lo siento, perdiste.') |
ventana = Tk() |
ventana.title('Premio') |
etiqueta = Label(ventana, text='¿Qué botón es el del premio?') |
etiqueta.pack() |
for boton inrange(1, 4): |
Button(text='Botón '+str(boton), command=lambdax=boton: apostar(x)).pack(side=LEFT) |
# Si pusiéramos 'command=apostar(boton)' se ejecutaría la función. |
ventana.mainloop() |
#!/usr/bin/env python |
# -*- coding: utf-8 -*- |
try: |
# Python 2.x |
from Tkinter import* |
from ttk import* |
from tkMessageBox import* |
exceptImportError: |
# Python 3.x |
from tkinter import* |
from tkinter.ttk import* |
from tkinter.messagebox import* |
# Ver http://www.python-course.eu/tkinter_dialogs.php |
contador =0 |
defcontar(): |
global contador |
contador +=1 |
etiqueta.configure(text=contador) |
defsaludar(): |
showinfo(title='Saludo', message='Hola '+texto.get()) |
ventana = Tk() |
ventana.title('Título de la ventana') |
ventana.geometry('300x400') |
imagen = PhotoImage(file='imagen.png') # Conseguir una de ejemplo |
Label(ventana, image=imagen).grid() |
etiqueta = Label(ventana, text='Etiqueta') |
etiqueta.grid() |
Button(ventana, text='Contar', command=contar).grid() |
texto = StringVar() |
Entry(ventana, textvariable=texto).grid() |
Button(ventana, text='Saludar', command=saludar).grid() |
ventana.mainloop() |