Text position in Tkinter

Asked

Viewed 2,285 times

0

Hello. I am working with Tkinter and I am at an impasse. I have nine buttons and each one prints a number. How can I make it so that after I press a button, no matter what button I press, a number always appears in the same position and when I press another button, no matter what button I press, another number appears next to the first one? Code:

# -*- coding: cp1252 -*-

from Tkinter import *
root = Tk()
root.minsize(620, 500)
root.title("Euromilhões virtual")
root.configure(background = 'blue')

def sorteio():
    import time
    import random
    numeros = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
    randw = random.sample(numeros, 1)
    randx = random.sample(numeros, 1)
    randy = random.sample(numeros, 1)
    randz = random.sample(numeros, 1)
    randt = random.sample(numeros, 1)
    mostrar = Label(root, text = randw, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 110, y = 250) 
    mostra = Label(root, text = randx, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 190, y = 250)
    amostra = Label(root, text = randy, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 270, y = 250)
    show = Label(root, text = randz, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 350, y = 250)
    visto = Label(root, text = randt, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 430, y = 250)

#Botões dos números:
def um():
    one = Label(root, text = "1", bg = "blue", font = ('arial', 20))
def dois():
    two = Label(root, text = "2", bg = "blue", font = ('arial', 20)) 
def tres():
    three = Label(root, text = "3", bg = "blue", font = ('arial', 20))
def quatro():
    four = Label(root, text = "4", bg = "blue", font = ('arial', 20))
def cinco():
    five = Label(root, text = "5", bg = "blue", font = ('arial', 20))
def seis():
    six = Label(root, text = "6", bg = "blue", font = ('arial', 20))
def sete():
    seven = Label(root, text = "7", bg = "blue", font = ('arial', 20))
def oito():
    eight = Label(root, text = "8", bg = "blue", font = ('arial', 20))
def nove():
    nine = Label(root, text = "9", bg = "blue", font = ('arial', 20))

btn_um = Button(root, width = 5, text = "1", fg = "red", command = um).place(x = 90, y = 12)
btn_dois = Button(root, width = 5, text = "2", fg = "red", command = dois).place(x = 140, y = 12)
btn_tres = Button(root, width = 5, text = "3", fg = "red", command = tres).place(x = 190, y = 12)
btn_quatro = Button(root, width = 5, text = "4", fg = "red", command = quatro).place(x = 240, y = 12)
btn_cinco = Button(root, width = 5, text = "5", fg = "red", command = cinco).place(x = 290, y = 12)
btn_seis = Button(root, width = 5, text = "6", fg = "red", command = seis).place(x = 340, y = 12)
btn_sete = Button(root, width = 5, text = "7", fg = "red", command = sete).place(x = 390, y = 12)
btn_oito = Button(root, width = 5, text = "8", fg = "red", command = oito).place(x = 440, y = 12)
btn_nove = Button(root, width = 5, text = "9", fg = "red", command = nove).place(x = 490, y = 12)
#-----------------------------------------------------

nums_aposta = Label(root, text = "Números escolhidos:", bg = "blue", fg = "black").place(x = 250, y = 40)

botao_lotaria = Button(root, width = 20, height = 2, text = "Iniciar Lotaria", bg = "yellow", command = sorteio).place(x = 235, y = 150)
root.mainloop()

1 answer

1


From what I understand, you need 5 Abels to display 5 numbers side by side. If that’s right, instead of creating 9 Labels, one for each button, you can create 5 without any definition of text, but associated with a Stringvar. As long as the Stringvar values are not defined, nothing is displayed. Below follows a very crude example (no layout) of how the thing works:

from Tkinter import *

cont = 1
lim = 5

def insereNumero(valor):
    global cont
    global lim
    if cont <= lim:
        eval('num' + str(cont) + '.set(' + valor + ')')
        cont += 1
    else:
        num1.set("Chega!!!")
        num2.set("Chega!!!")
        num3.set("Chega!!!")
        num4.set("Chega!!!")
        num5.set("Chega!!!")

gui = Tk()

gui.title("Teste Python+Tkinter")
gui.geometry('300x400')

num1 = StringVar()
num2 = StringVar()
num3 = StringVar()
num4 = StringVar()
num5 = StringVar()

btn1 = Button(gui, text="1", command= lambda: insereNumero('1')).pack()
btn2 = Button(gui, text="2", command= lambda: insereNumero('2')).pack()
btn3 = Button(gui, text="3", command= lambda: insereNumero('3')).pack()
btn4 = Button(gui, text="4", command= lambda: insereNumero('4')).pack()
btn5 = Button(gui, text="5", command= lambda: insereNumero('5')).pack()
btn6 = Button(gui, text="6", command= lambda: insereNumero('6')).pack()
btn7 = Button(gui, text="7", command= lambda: insereNumero('7')).pack()
btn8 = Button(gui, text="8", command= lambda: insereNumero('8')).pack()
btn9 = Button(gui, text="9", command= lambda: insereNumero('9')).pack()

label1 = Label(gui, textvariable=num1).pack()
label2 = Label(gui, textvariable=num2).pack()
label3 = Label(gui, textvariable=num3).pack()
label4 = Label(gui, textvariable=num4).pack()
label5 = Label(gui, textvariable=num5).pack()

gui.mainloop()

Browser other questions tagged

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