Python tkinter .config 失败

Python tkinter .config fails

我正在尝试制作一个简单的 window 来显示有关使用 tkinter 的 2b2t minecraft 服务器队列中剩余位置和时间的详细信息。 我的问题是我无法更新文本,我得到了一个莫名其妙的错误。 该程序在没有 GUI 部分的情况下已经完美运行。我在 Whosebug 上找不到匹配的 post,但如果有,请 link 它。如果有人可以提供帮助,我会很好。 亲切的问候, 菲利普

我的代码:


   
import time
from math import *
from tkinter import *

#window
window = Tk()
window.title("2b2t queue")
window.resizable(False, False)
window.geometry("500x500")
window.configure(background="#101010")

WindowTitle = Label(window, text="2b2t queue", font=("Helvetica", 30), bg="#101010", fg="white")
WindowTitle.place(x=150, y=0)

WindowPlaceInQueue = Label(window, text="Place in queue:", font=("Helvetica", 20), bg="#101010", fg="white")
WindowPlaceInQueueNumber = Label(window, text="wait", font=("Helvetica", 20), bg="#101010", fg="white")
WindowPlaceInQueue.place(x=150, y=100)
WindowPlaceInQueueNumber.place(x=200, y=150)


WindowMovedPlaces = Label(window, text="Moved Places:", font=("Helvetica", 20), bg="#101010", fg="white")
WindowMovedPlacesNumber = Label(window, text="wait", font=("Helvetica", 20), bg="#101010", fg="white")
WindowMovedPlaces.place(x=150, y=220)
WindowMovedPlacesNumber.place(x=200, y=270)


WindowTimeLeft = Label(window, text="Time left:", font=("Helvetica", 20), bg="#101010", fg="white")
WindowTimeLeftNumber = Label(window, text="wait", font=("Helvetica", 20), bg="#101010", fg="white")
WindowTimeLeft.place(x=150, y=340)
WindowTimeLeftNumber.place(x=200, y=390)



def check ():
   newestlog = open(r"\Users\MSI Gaming\AppData\Roaming\.minecraft\logs\latest.log", "r")
   lastnumber = newestlog.read()

   number = lastnumber[-4]
   number += lastnumber[-3]
   number += lastnumber[-2]

   newestlog.close()
   FinalPlace = number
   #print(FinalPlace)
   return FinalPlace



TimeStart = time.time()
FirstRound = True

PlaceInQueue = 0
FirstPlace = 0
TimeLeft = 0
def loop():
   global FirstRound
   global PlaceInQueue
   global FirstPlace
   global TimeLeft
   check()
   try:
       PlaceInQueue = float(check())
       print(PlaceInQueue)
   except:
       print("not a number")

   if FirstRound:
       print("First round")
       FirstPlace = PlaceInQueue
       FirstRound = False

   MovedPlaces = FirstPlace - PlaceInQueue
   print(MovedPlaces)

   try:
       TimePassed = time.time() - TimeStart
       TimeLeft = TimePassed / MovedPlaces * PlaceInQueue
       TimeLeft = floor(TimeLeft / 60)
       Hour = floor(TimeLeft / 60)
       Minute = TimeLeft - Hour * 60
       print(Hour, "h", Minute, "m")
       print(TimeLeft)
   except ZeroDivisionError:
       print("Pls wait!")

   #window
   #global WindowPlaceInQueueNumber
   #global WindowMovedPlacesNumber
   #global WindowTimeLeftNumber

   WindowPlaceInQueueNumber.config(window, text=PlaceInQueue)
   WindowMovedPlacesNumber.config(window, text=MovedPlaces)
   WindowTimeLeftNumber.config(window, text=TimeLeft)
   print(PlaceInQueue)
   print(MovedPlaces)
   print(TimeLeft)

   print("-----------------------------------------")
   window.after(1000, loop)





window.after(1000, loop)
window.mainloop()

输出:

35.0
First round
0.0
Pls wait!
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\MSI Gaming\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\MSI Gaming\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 814, in callit
    func(*args)
  File "C:\Users\MSI Gaming\PycharmProjects\stuffb2t_queue_calculator.py", line 92, in loop
    WindowPlaceInQueueNumber.config(window, text=PlaceInQueue)
  File "C:\Users\MSI Gaming\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1646, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Users\MSI Gaming\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1636, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-class"

错误发生在这里(以及后面的两个语句):

WindowPlaceInQueueNumber.config(window, text=PlaceInQueue)

config 方法采用命名参数。您需要删除 window.