更新或删除有条件的标签

问题描述 投票:0回答:2

我需要删除或更改函数“obtener_info”的标签,我不知道如何在 tkinter 中更新或删除带有条件的标签。有人知道怎么做吗? 这是我的代码,我正在使用 pyserial 和心跳传感器。这是一个学校项目。

............................................... .....................................

import tkinter as tk
from tkinter import ttk
from tkinter import *
import serial

arduino = serial.Serial('COM4', 9600)

class Proyecto(tk.Tk):#Contains the methods of the window
    def __init__(self, window) : #initializes the state of an object
        self.wind = window #store the window as a parameter
        self.wind.title('Proyecto')#title of the windows
        self.wind.geometry('600x400+375+50') #window dimensions
        self.wind.resizable(False, False) #remove the maximize option
        self.windtext = Label(self.wind, text='Calculadora de salud', font="Century_Gothic")#title
        self.windtext.pack(ipady= 30)#position
#a container is created
        frame = LabelFrame(self.wind, text ='Ingrese sus datos', font="Century_Gothic") #name of the container
        frame.pack(ipady= 20, ipadx = 40, anchor= tk.N) #position of the container
#input age
        self.labeledad = Label(frame, text= 'Edad')#text that appears in front of the input
        self.labeledad.pack(ipady=15, ipadx = 0)#position text
        self.edad = ttk.Combobox(frame, state='readonly') #declare combobox
        self.edad.pack(ipady=0.5, ipadx= 2)#position of the combobox
        self.edad['values'] = ('20 a 29',  '29 a 39', '39 a 49', '+50')#values of the combobox
        self.edad.current(0) #default value
        self.edad.pack(ipady=  0, ipadx= 0) #position input
        self.edad.focus()
#Combobox Gender
        
        self.labelgenero = Label(frame, text = 'Genero')  #text that appears in front of the input
        self.labelgenero.pack(ipady=  15, ipadx= 0) #position text
        self.genero = ttk.Combobox(frame, state='readonly') #declare combobox
        self.genero.pack(ipady=0.5, ipadx= 2)#position of the combobox
        self.genero['values'] = ('masculino',  'femenino')#values of the combobox
        self.genero.current(0) #default value
#send data button
        def obtener_info():
                genero = self.genero.get()#save the variables of the gender field
                edad = self.edad.get()#save the variables of the age field
                bpm = int(arduino.readline().decode('utf-8')) #comunication with arduino monitor serial
                NewWind = Toplevel(window) #open a new window
                NewWind.geometry('500x500')#new window dimensions
                NewWind.title('Coso')#title new window
                self.wind.withdraw()
                NewWind.update()
                while True:
                        bpm = int(arduino.readline().decode('utf-8')) #comunication with arduino monitor serial
                        Label(NewWind, text=bpm, font="Century_Gothic").place(x=0, y=0) #BPM in tkinter window
                        print(bpm)
                        NewWind.update() #window update
                        if genero == "femenino":
                                Label(NewWind, text="Su estado de salud es", font="Century_Gothic").place(x=100, y=100)
                                if edad == "20 a 29":
                                        if bpm >= 78 and bpm <= 94:
                                                Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200)
                                        elif bpm < 78 or bpm > 94:
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300)               
                                elif edad == "29 a 39":
                                        if bpm >= 80 and bpm <= 96:
                                            Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200)  
                                        elif bpm < 80 and bpm > 96:
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300)  
                                elif edad == "39 a 49":
                                        if bpm >= 80 and bpm <= 98:
                                               Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200)     
                                        elif bpm > 80 and bpm < 98:  
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300)          
                                elif edad == "+50":
                                        if bpm >= 84 and bpm <= 102:
                                               Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200) 
                                        elif bpm > 84 and bpm < 102:
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300)    
                                
                        if genero == "masculino":
                                Label(NewWind, text="Su estado de salud es", font="Century_Gothic").place(x=100, y=100)
                                if edad == "20 a 29":
                                        if bpm >= 70 and bpm <= 84:
                                                        Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200)
                                        elif bpm < 74 or bpm > 84:
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300)               
                                elif edad == "29 a 39":
                                        if bpm >= 74 and bpm <= 84:
                                            Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200)  
                                        elif bpm < 74 and bpm > 84:
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300)  
                                elif edad == "39 a 49":
                                        if bpm >= 74 and bpm <= 88:
                                               Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200)     
                                        elif bpm > 74 and bpm < 88:  
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300)          
                                elif edad == "+50":
                                        if bpm >= 76 and bpm <= 88:
                                               Label(NewWind, text="optima", font="Century_Gothic").place(x=100, y=200) 
                                        elif bpm > 76 and bpm < 88:
                                                Label(NewWind, text="no optima", font="Century_Gothic").place(x=100, y=300) 

        self.boton = Button(frame, text = 'Enviar Datos', command=obtener_info).pack(ipady =0, ipadx= 36, pady=30) #create the button that sends the data
                

if __name__  == '__main__' or 'NewWind':
    window = Tk() 
    aplication = Proyecto(window) 
    window.mainloop() 
python tkinter pyserial
2个回答
0
投票

我在每个条件下都放置了一个 label.configure,现在它会更改 arduino 的每个更新。这是更新的代码。

if genero == "femenino":
                                    label_estado = Label(NewWind, text="Su estado de salud es ", font="Century_Gothic")
                                    label_estado.place(x=100, y=100)
                                    if edad == "20 a 29":
                                            if bpm >= 78 and bpm <= 94:
                                                    label_estado.configure(text="Su estado de salud es optimo")
                                            elif bpm < 78 or bpm > 94:
                                                    label_estado.configure(text="Su estado de salud es no optimo")

0
投票

您正在重复使用

Label
小部件并浪费您所有的 CPU 内存。 注释掉
#self.wind.withdraw()
。你正在破坏
Toplevel
窗口。 将
Label
小部件放在
while/else
块之外,并重命名为
lbl_data
。 使用
lbl_data.congigure
.

片段:

def obtener_info():
    genero = self.genero.get()#save the variables of the gender field
    edad = self.edad.get()#save the variables of the age field
    bpm = int(arduino.readline().decode('utf-8')) #comunication with arduino monitor serial
    NewWind = Toplevel(window) #open a new window
    NewWind.geometry('500x500')#new window dimensions
    NewWind.title('Coso')#title new window
    lbl_data = Label(NewWind, text=bpm, font="Century_Gothic")
    lbl_data.place(x=0, y=0) #BPM in tkinter window
    #self.wind.withdraw()
    NewWind.update()
    while True:
        bpm = int(arduino.readline().decode('utf-8')) #comunication with arduino monitor serial
        data = bpm 
        print(bpm)
        NewWind.update() #window update
        if genero == "femenino":
            data = "Su estado de salud es"
            if edad == "20 a 29":
                if bpm >= 78 and bpm <= 94:
                    data = "optima" 
                elif bpm < 78 or bpm > 94:
                    data = "no optima"                
            elif edad == "29 a 39":
                if bpm >= 80 and bpm <= 96:
                    data = "optima"  
                elif bpm < 80 and bpm > 96:
                    data = "no optima"  
            elif edad == "39 a 49":
                if bpm >= 80 and bpm <= 98:
                    data = "optima"     
                elif bpm > 80 and bpm < 98:  
                    data = "no optima"          
            elif edad == "+50":
                if bpm >= 84 and bpm <= 102:
                    data = "optima" 
                elif bpm > 84 and bpm < 102:
                    data = "no optima"     
            
        if genero == "masculino":
            data = "Su estado de salud es" 
            if edad == "20 a 29":
                if bpm >= 70 and bpm <= 84:
                    data = "optima" 
                elif bpm < 74 or bpm > 84:
                    data = "no optima"                
            elif edad == "29 a 39":
                if bpm >= 74 and bpm <= 84:
                    data = "optima"  
                elif bpm < 74 and bpm > 84:
                    data = "no optima"  
            elif edad == "39 a 49":
                if bpm >= 74 and bpm <= 88:
                    data = "optima"      
                elif bpm > 74 and bpm < 88:  
                    data = "no optima"          
            elif edad == "+50":
                if bpm >= 76 and bpm <= 88:
                    data = "optima" 0) 
                elif bpm > 76 and bpm < 88:
                    data = "no optima" 
        lbl_data.configure(text=data)
© www.soinside.com 2019 - 2024. All rights reserved.