Search code examples
pythontkintertkinter-text

Python tkinter .config fails


I am trying to make a simple window that schows details about the position and time left in queue of the 2b2t minecraft server with tkinter. My problem is that I cannot update the text and I get a for me inexplicable error. The program already runed perfectly without the GUI part. I could't find a matching post on stackoverflow but if there is one please link it. I would be nice if someone can help. In kind regards, Philipp

My code:


   
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()

Output:

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\stuff\2b2t_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"

Solution

  • The error is happening here (and the two statements that follow it):

    WindowPlaceInQueueNumber.config(window, text=PlaceInQueue)
    

    The config method takes named parameters. You need to remove window.