【问题标题】:my tkinter window appears after I have ended the program结束程序后出现我的 tkinter 窗口
【发布时间】:2020-10-15 22:59:58
【问题描述】:

我正在使用树莓派制作气象站,我正在尝试使用 tkinter 窗口来显示数据,但是,该窗口仅在我使用 ctrl + c 结束程序后才会出现。我正在使用终端运行代码(命令是sudo python test.py)

有谁知道我做错了什么?

from WeatherPiHumiture import *
from WeatherPiBarometer import *
import Tkinter as tkinter

window = tkinter.Tk()

def main():
    while True:
                result = read_dht11_dat()
                if result:
                        humidity, temperature = result
                        humid = "Humidity: %s %% " % (humidity)
                        humids = tkinter.Label(
                            text=humid,
                            fg='black',
                            bg='white',
                            height=2,
                            width=40
                        )

                #Barometer
                sensor = BMP085.BMP085()
                temp = sensor.read_temperature()    # Read temperature to veriable temp
                pressure = sensor.read_pressure()
                
                temperatures = 'Temperature: {0:0.2f} C'.format(temp)# Print temperature
                temperate = tkinter.Label(
                        text=temperatures,
                        fg='black',
                        bg='white',
                        height=2,
                        width=40
                )
            
                pressure = '{0:0.2f}'.format(pressure)
                pressure = float(pressure)
                millibar = pressure / 100
                millibar = str(millibar)
                pressured = 'Pressure: ' + millibar + ' MilliBar'
                pressures = tkinter.Label(
                        text=pressured,
                        fg='black',
                        bg='white',
                        height=2,
                        width=40
                )

                temperate.grid(row=0, column=0)
                pressures.grid(row=0, column=1)
                humids.grid(row=0, column=2)
                
                time.sleep(1)

def destroy():
    GPIO.cleanup()

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        destroy()


window.mainloop()

【问题讨论】:

  • 为什么要定义 main?它不是 c++ xD
  • @Evorage:创建main 函数很常见,即使在 python 中也是如此。
  • 尝试将父窗口作为父窗口传递给标签。例如 tkinter.Label(window, 然后是其他 args)
  • @tan_an 这就是你的意思humids = tkinter.Label( master=window.mainloop(), text=humid, fg='black', bg='white', height=2, width=40 )
  • 它应该是 humids=tkinter.Label(window, text=humid, fg='black', bg='white')。这将添加您在开始时创建的窗口内的标签。

标签: python python-3.x tkinter raspberry-pi4


【解决方案1】:

window.mainloop() 添加到您的try 块中

if __name__ == '__main__':
    try:
        main()
        window.mainloop()
    except KeyboardInterrupt:
        destroy()
        window.destroy()

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 2018-09-20
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    相关资源
    最近更新 更多