User choices in Tkinter

0

Hello. I'm trying to create a graphical interface program using Tkinter (I learned a short time ago) and wanted the user to write something in the text input box and press the button. If you write capital B it prints "starting the bios" and if it does not write something it prints "to start the OS". Code:

# -*- coding: cp1252 -*-
from Tkinter import *

#função de arranque da VM:
def arranque():
    root = Tk()
    root.minsize(950, 700)
    root.wm_iconbitmap('virtual-pc-1338544131.ico')
    root.title("Computador virtual PowerBIT v1.0")
    root.configure(background='black')
    machine = Label(root, text="Bem vindo à máquina virtual. Prima B para aceder ao BIOS ou ENTER para inciar o SO", fg="white", bg="black").grid(row=0)
    e1 = Entry(root, bg = "white", fg = "black" )
    e1.grid(row=0, column=1)
    botao_de_arranque = Button(root, text="Iniciar", width=6).grid(row = 1, column = 1, ) 
    root.mainloop()
arranque()
    
asked by anonymous 22.06.2014 / 19:12

1 answer

1

widget Button provides a callback that allows you to do something when the user clicks the button.

See an example:

from Tkinter import *

def callback():
    print "O usuario clicou no botao"

b = Button(text="Clique aqui", command=callback)
b.pack()

mainloop()

In the code you presented, this could be implemented as follows:

# -*- coding: cp1252 -*-
from Tkinter import *

def btn_arranque(texto):
    # Fazer alguma coisa aqui ao clicar no botão
    if texto == 'B':
        # Fazer alguma coisa aqui caso o edit ter o texto 'B'
    else:
        # Fazer algo aqui caso o texto do edit não for 'B'

def arranque():
    root = Tk()
    root.minsize(950, 700)
    root.wm_iconbitmap('virtual-pc-1338544131.ico')
    root.title("Computador virtual PowerBIT v1.0")
    root.configure(background='black')

    machine = Label(root, text="Bem vindo à máquina virtual. Prima B para aceder ao BIOS ou ENTER para inciar o SO", fg="white", bg="black").grid(row=0)
    e1 = Entry(root, bg="white", fg="black" )
    e1.grid(row=0, column=1)
    botao_de_arranque = Button(root, text="Iniciar", command= lambda: btn_arranque(e1.get()), width=6).grid(row=3, column=1,) 
    root.mainloop()

arranque()
    
22.06.2014 / 23:13