如何在Tkinter窗口中显示文本变量?

2024-09-30 14:17:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做热成像相机,我在做界面。我面临一些错误,我的textvariable不能在tkinter中打印,但仍然可以打印在shell上。我的代码怎么了

import pandas as pd
import cv2
import matplotlib.pyplot as plt
from tkinter import *
from tkinter.filedialog import askopenfilenames

# List of list
images = []
gs = []
B = []
G = []
R = []
suhu = []
suhuo = []
red = []
green = []
blue = []
waktu = []
utama = Tk()

# Fungsi-fungsi
def mouseRGB(event,x,y,flags,param):
    global gambar
    if event == cv2.EVENT_LBUTTONDOWN: 
        colorsB = gambar[y,x,0]
        colorsG = gambar[y,x,1]
        colorsR = gambar[y,x,2]
        R.append(colorsR)
        G.append(colorsG)
        B.append(colorsB)

def Tutup ():
    utama.destroy()

def tolong ():
    messagebox.showinfo('Help','Jika terjadi error atau kesulitan dalam menggunakan program ini dapat menghubungi 08111903747')

def loadImages ():
    global wkt
    global gambar
    global tem
    wkt = float(wktu.get())
    Gambar = askopenfilenames(parent=jwaktu,
                                 title='Select a thermal image file',
                                 filetypes=[('JPG File','*.jpg'),
                                            ('PNG File','*.png'),
                                            ('All Files','*')])
    # Tutorial pake aplikasi
    messagebox.showinfo('How to use','After clicking or dragging the selected picture, press S for next image')

    # Program olah suhu
    # Perintah Load gambar ke python
    filenames = Gambar
    # Jendela Mouse
    cv2.namedWindow('mouseRGB')
    cv2.setMouseCallback('mouseRGB',mouseRGB)
    time = 0
    for file in filenames:
        gambar = cv2.imread(file)
        time += wkt
        cv2.imshow('mouseRGB',gambar)
        while(1):
            if cv2.waitKey(0) & 0xFF == ord('s') :
                for r in R:
                    red.append(float(r))
                for g in G:
                    green.append(float(g))
                for b in B:
                    blue.append(float(b))
                nr = [i * 0.020411398053616 for i in red]
                ng = [j * -0.01805397732321 for j in green]
                nb = [k * -0.06212844019856 for k in blue]
                R.clear()
                G.clear()
                B.clear()
                red.clear()
                green.clear()
                blue.clear()
                lum = nr + ng + nb 
                lumi = sum(lum)
                tem = lumi + 34.9927907296913
                if tem >= 33.0 and tem<= 39.0:
                    suhuo.append(tem)
                    waktu.append(time)
                suhu.append([file,time,tem])
                SuhuA = Label(jwaktu,textvariable = tem)
                SuhuA.grid(column=1, row = 1)
                print(tem)
                break

    cv2.destroyAllWindows()

    # Olah grafik
    plt.plot(waktu,suhuo)
    plt.xlabel('Waktu (s)')
    plt.ylabel('Suhu (C)')
    plt.title('Sebaran suhu')
    plt.savefig('Sebaran_suhu.png')
    plt.show()
    # Masukan data kedalam csv
    img_df = pd.DataFrame(suhu ,columns = ['Image','Waktu','Temperature'])
    img_df.to_csv('Olah_Suhu.csv')
    waktu.clear()
    suhuo.clear()


def TIP ():
    global SuhuA
    global wktu
    global jwaktu
    jwaktu = Tk()
    jwaktu.title('Thermal Image Processing')
    jwaktu.geometry('600x400')
    Tulisanw = Label(jwaktu,text='Enter the sampling time : ')
    Tulisanw.grid(column = 0 , row = 0)
    wktu = Entry(jwaktu,width = 10)
    wktu.grid(column = 1, row = 0)
    tombolw = Button(jwaktu,text='Execute',command=loadImages)
    tombolw.grid(column = 2, row = 0)
    Suhu = Label(jwaktu,text='Temperature Point   : ')
    Suhu.grid(column = 0, row = 1)
    jwaktu.mainloop()
    return wktu


# Window GUI
utama.title('TCI - Thermograpgy Camera Interface')
utama.geometry('600x400')
menu = Menu(utama)
program = Menu(menu)
program.add_command(label = 'Close',command=Tutup)
menu.add_cascade(label='File', menu=program)
menu.add_command(label = 'Help',command=tolong)
utama.config(menu=menu)
TombolTIP = Button(utama,width=30,text='Thermal Image Processing',command=TIP)
TombolTIP.grid(padx = 200,pady = 50)
TombolRTM = Button(utama,width=30,text = 'Realtime Thermal Measurment')
TombolRTM.grid(padx=200,pady=150)
utama.mainloop()

当我在另一个窗口执行时,温度点没有显示任何东西。我已经尝试使用“text”而不是“textvariable”,但是什么也没发生。谢谢你的帮助


Tags: textinforpltcv2globalgridmenu

热门问题