我正在嘗試制作一個簡單的視窗,顯示有關使用 tkinter 的 2b2t minecraft 服務器佇列中剩余位置和時間的詳細資訊。我的問題是我無法更新文本,并且收到了一個莫名其妙的錯誤。該程式在沒有 GUI 部分的情況下已經完美運行。我在 stackoverflow 上找不到匹配的帖子,但如果有請鏈接。如果有人可以提供幫助,我會很好。親切的問候,菲利普
我的代碼:
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\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"
uj5u.com熱心網友回復:
錯誤發生在這里(以及它后面的兩個陳述句):
WindowPlaceInQueueNumber.config(window, text=PlaceInQueue)
該config方法采用命名引數。您需要洗掉window.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398956.html
上一篇:使用columnconfigure(全域columnconfigure?)后如何在同一行中排列多個專案
下一篇:如何從最近到最近的日期排序
