User Choices in Tkinter

Asked

Viewed 388 times

0

Hello. I am trying to create a program of graphical interface using Tkinter(I learned a little while ago) and I wanted the user, when writing in the text input box and pressing the button he printed something. If you type capital B, print "starting bios" and if you don’t write something print "starting 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()
  • I’ll give you a hint, buddy. Use easygui.

1 answer

1


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

Take 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 presented by you 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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.