Tkinter窗口在单击时崩溃,可能是由于无限循环所致。的Python 3.7.1

时间:2018-12-11 14:33:54

标签: python python-3.x tkinter

我正在尝试创建一个程序,该程序读取ANT信号并将其显示在TKinter窗口中。 为了显示变化的值,我正在更新标签(无限循环),该标签到目前为止有效,但是每当我尝试移动窗口时,程序就会崩溃(无响应)。 因此,(最终程序)应该执行以下操作:

  • 接收并存储ANT数据,该数据是我通过另一个文件读取的(这给了我 力量和节奏)
  • 将数字四舍五入到2-3个小数(最大)
  • 从文件读取到目前为止的总功率
  • 在文件中写入功率数据(附加)
  • 总输出功率
  • 显示当前速度,节奏,功率和总功率输出

我的代码:

# imports
import random
import time
from tkinter import *

# creating root tkinter Window
root = Tk()
root.title("ANT+ Live Data")

# defining Text Variables
running = 0
SAVE_FILE = "power_doc.txt"
HEADLINE_TEXT = StringVar()
HEADLINE_TEXT.set('')
HEADLINE_M = StringVar()
HEADLINE_M.set('')
power_display = StringVar()
power_display.set('0')
cadence_display = StringVar()
cadence_display.set('0')
speed_display = StringVar()
speed_display.set('0')
powertotal_display = StringVar()
powertotal_display.set('0')

# defining Window Size
root.geometry("800x600")

# creating Label Frame to wrap labels
frame = LabelFrame(root)
frame.pack(padx=20, pady=20)

# creating labels, Headline 1
slabel = Label(root, font = ("Helvetica",48) ,textvariable = HEADLINE_M, justify=CENTER, bg="#FFFFFF", fg = '#000000')
slabel.pack(fill=BOTH, padx=20, pady=10)

# headline 2
headLine = Label(root, font=("Helvetica", 26), textvariable= HEADLINE_TEXT, justify=CENTER, bg="#FFFFFF", fg = '#000000')
headLine.pack(fill=BOTH, pady=5)

# power
l1 = Label(root, font = ("Helvetica",20) ,textvariable = power_display, justify=CENTER, bg="#190000", fg = '#B0C4C2')
l1.pack(fill=BOTH)

# cadence
l2 = Label(root, font = ("Helvetica",20), textvariable = cadence_display, justify=CENTER, bg="#190000", fg = '#B0C4C2')
l2.pack(fill=BOTH)

# speed
l3 = Label(root, font = ("Helvetica",20), textvariable = speed_display, justify=CENTER, bg="#190000", fg = '#B0C4C2')
l3.pack(fill=BOTH)

# total Power
l4 = Label(root, font = ("Helvetica",20), textvariable = powertotal_display, justify=CENTER, bg="#190000", fg = '#B0C4C2')
l4.pack(fill=BOTH)


# function to read total power values out of a file
def get_power_total(SAVE_FILE):
    power_total = 0
    with open(SAVE_FILE, 'r') as inp:
        for line in inp:
            try:
                num = float(line)
                power_total += num
            except ValueError:
                print('{} is not a number!'.format(line))
    power_total = round_number(power_total)
    return power_total


# generate random Numbers for testing purposes
def randomize_number(inputNumber):
    inputNumber = float(inputNumber)*random.uniform(0.99, 1.01)
    inputNumber = round_number(inputNumber)
    return inputNumber


# rounds numbers to 2 decimals
def round_number(inputNumber):
    inputNumber = round(inputNumber, 2)
    return inputNumber


# use formula for a 28" Wheel to determine Speed using cadence
def determine_speed(cadence):
    speed = float(cadence)*0.356
    speed = round_number(speed)
    return speed


def get_power(cadence):
    power = float(cadence)*0.8
    power = round_number(power)
    return power


# dummy function to test-print Values, redundant in current state
def print_values(x1, x2, x3):
    print("Leistung: {} W Trittfrequenz: {} Geschwindigkeit: {} Km/H" .format(x1, x2, x3))


# write in document to determine total power output
def write_file(SAVE_FILE, power):
    f = open(SAVE_FILE, "a+")
    f.write(str(power) + '\n')
    f.close()


def update_label():
    power_total = get_power_total(SAVE_FILE)
    cadence = int(50)
    cadence = randomize_number(cadence)
    root.after(100)
    power = get_power(cadence)
    root.after(100)
    speed = determine_speed(cadence)
    root.after(100)
    power_total += power
    write_file(SAVE_FILE, power)
    power_display.set("Leistung: {} W".format(power))
    cadence_display.set("Trittfrequenz: {}".format(cadence))
    speed_display.set("Geschwindigkeit : {} Km/h".format(speed))
    powertotal_display.set("Gesamtleistung seit 10 Uhr: {} W".format(power_total))
    root.after(1000)
    root.update_idletasks()


# mainloop to display the window
while True:

        root.after(1000, update_label())

root.mainloop()

这基本上只是导入ANT数据的草稿,因此会随机生成一些数字以显示标签更新功能是否起作用。 我对使用Python编码(或完全编码)还不陌生,因此我可以肯定我的代码也存在其他问题,但是也许有人对如何解决我的问题提出了建议。 任何帮助或建议,表示赞赏。

提前加油!

编辑:已解决!谢谢杰森!

要在此处进行更改:

def update_label():
    power_display.set("Leistung: {} W".format(power))
    cadence_display.set("Trittfrequenz: {}".format(cadence))
    speed_display.set("Geschwindigkeit : {} Km/h".format(speed))
    powertotal_display.set("Gesamtleistung seit 10 Uhr: {} W".format(power_total))
    root.after(1000, update_label)
    root.update_idletasks()

root.after(1000, update_label)
root.mainloop()

0 个答案:

没有答案