Python - centrar botones y texto en GUIZERO python

 
Vista:

centrar botones y texto en GUIZERO python

Publicado por lucía (7 intervenciones) el 29/03/2023 19:36:01
Buenas tardes. Necesito centrar el texto y los botones en el centro de la ventana. Lo que tengo ahora mismo en mi programa es esto:
(si le dais a ejecutar os dareis cuenta de cuál es el error, el texto me sale a la izquierda, las cajitas en el centro y el otro botón en la derecha y quiero que esté todo centrado)

from guizero import App, Text, Box, TextBox, PushButton, Picture

# App -----------------------
app = App(bg = "light blue",
title ="GTDM",
height = 550,
width = 650)
app.align = "center"

grid_box = Box(app, layout="grid")


# Functions and variables ---
peso = ""

def get_peso(e):
global peso
input_asked = "peso"
if (e == '\r'):
#tenemos el peso: clear text box and clear list
text_peso.clear()
text_peso.append("Tu peso es: " + peso)
textbox_peso.clear()
peso = ""
elif (e.isdigit()):
peso = peso + e
print(e)
print(peso)
else:
#no es una entrada valida
text_peso.clear()
peso = ""
app.info("error", "no es " + input_asked + " valida")

altura = ""

def get_altura(e):
global altura
input_asked = "altura"
if (e == '\r'):
#tenemos la edad: clear text box and clear list
text_altura.clear()
text_altura.append("Tu altura es: " + altura)
textbox_altura.clear()
altura = ""
elif (e.isdigit()):
altura = altura + e
print(e)
print(altura)
else:
#no es una entrada valida
text_altura.clear()
altura = ""
app.info("error", "no es " + input_asked + " valida")

bmi = 0

def calcular_bmi():
if ((altura != "") and (peso != "")):
global bmi
l = int (altura)
p = int (peso)
bmi = p / ((l/100)**2)
text_bmi.clear()
text_bmi.append("Tu BMI es: " + str('{0:.2f}'.format(bmi)))

if (bmi >= 25.0 and bmi <= 29.9):
tipo_bmi = "Sobrepeso (no obesidad)"
elif (bmi >= 30 and bmi <= 34.9):
tipo_bmi = "Obesidad clase 1 (de bajo riesgo)"
elif (bmi >= 35 and bmi <= 39.9):
tipo_bmi = "Obesidad clase 2 (riesgo moderado)"
elif (bmi >= 40):
tipo_bmi = "Obesidad clase 3 (de alto riesgo)"

else:
app.info("error", "entra peso y altura valida")

# Widgets -----------------------
input_asked = "peso"

#text widget
text = Text(grid_box, text="Cual es tu " + input_asked, grid=[0,0])

#textbox widget
textbox_peso = TextBox(grid_box, command=get_peso, grid=[1,0])
textbox_peso.bg = "white"
text_peso = Text(grid_box, text="Tu " + input_asked + " es: " + peso, grid=[0,1])

input_asked = "altura"

#text widget
text = Text(grid_box, text="Cual es tu " + input_asked, grid=[0,2])
#textbox widget
textbox_altura = TextBox(grid_box, command=get_altura, grid=[1,2])
textbox_altura.bg = "white"
text_altura = Text(grid_box, text="Tu " + input_asked + " es: " + altura, grid=[0,3])

button = PushButton(grid_box, text = "Calcular BMI", width="10", height="5", grid=[2,2], command=calcular_bmi)

text_bmi = Text(grid_box, text="Tu BMI es: " + str(bmi), grid=[1,4])

picture3 = Picture(grid_box, image="bmi.jpg", width=400, height=200, grid=[1,6])

# Display -----------------------
app.display()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

centrar botones y texto en GUIZERO python

Publicado por antonio (65 intervenciones) el 30/03/2023 10:58:49
Hola buenas si te refieres a como se observa en la imagen:

Captura
El código es el siguiente, pero cuidado que según el tamaño de la imagen se centrara todo los textos, los input text y el botón al ancho de la imagen:
Captura2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from guizero import *
 
# App -----------------------
app = App(bg = "light blue",
title ="GTDM",
height = 550,
width = 650)
 
 
grid_box = Box(app, layout="grid")
 
 
# Functions and variables ---
peso = ""
 
def get_peso(e):
    global peso
    input_asked = "peso"
    if (e == '\r'):
        #tenemos el peso: clear text box and clear list
        text_peso.clear()
        text_peso.append("Tu peso es: " + peso)
        textbox_peso.clear()
        peso = ""
    elif (e.isdigit()):
        peso = peso + e
        print(e)
        print(peso)
    else:
    #no es una entrada valida
        text_peso.clear()
        peso = ""
        app.info("error", "no es " + input_asked + " valida")
 
altura = ""
 
def get_altura(e):
    global altura
    input_asked = "altura"
    if (e == '\r'):
        #tenemos la edad: clear text box and clear list
        text_altura.clear()
        text_altura.append("Tu altura es: " + altura)
        textbox_altura.clear()
        altura = ""
    elif (e.isdigit()):
        altura = altura + e
        print(e)
        print(altura)
    else:
        #no es una entrada valida
        text_altura.clear()
        altura = ""
        app.info("error", "no es " + input_asked + " valida")
 
bmi = 0
 
def calcular_bmi():
    if ((altura != "") and (peso != "")):
        global bmi
        l = int (altura)
        p = int (peso)
        bmi = p / ((l/100)**2)
        text_bmi.clear()
        text_bmi.append("Tu BMI es: " + str('{0:.2f}'.format(bmi)))
 
    if (bmi >= 25.0 and bmi <= 29.9):
        tipo_bmi = "Sobrepeso (no obesidad)"
    elif (bmi >= 30 and bmi <= 34.9):
        tipo_bmi = "Obesidad clase 1 (de bajo riesgo)"
    elif (bmi >= 35 and bmi <= 39.9):
        tipo_bmi = "Obesidad clase 2 (riesgo moderado)"
    elif (bmi >= 40):
        tipo_bmi = "Obesidad clase 3 (de alto riesgo)"
 
    else:
        app.info("error", "entra peso y altura valida")
 
 
 
#text widget
text = Text(grid_box, text="Cual es tu peso", grid=[0,1])
 
#textbox widget
textbox_peso = TextBox(grid_box, command=get_peso, grid=[1,1])
textbox_peso.bg = "white"
text_peso = Text(grid_box, text="Tu peso es: " + peso, grid=[0,2])
 
 
#text widget
text = Text(grid_box, text="Cual es tu altura", grid=[0,3])
#textbox widget
textbox_altura = TextBox(grid_box, command=get_altura, grid=[1,3])
textbox_altura.bg = "white"
text_altura = Text(grid_box, text="Tu altura es: " +str(altura), grid=[0,4])
 
button = PushButton(grid_box, text = "Calcular BMI", width="10", height="5", grid=[0,5], command=calcular_bmi)
 
text_bmi = Text(grid_box, text="Tu BMI es: " + str(bmi), grid=[1,5])
 
#picture3 = Picture(grid_box, image="bmi.jpg", width=400, height=200, grid=[0,6])
 
# Display -----------------------
app.display()
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar