如何解决Tkinter不允许我在画布上放置条目
当我自己运行此代码时,它什么也不做。但是当与第二组一起运行时,会产生一个非常奇怪的错误。它旨在创建一个窗口,要求输入一个条目,然后放置一个确认按钮,但该条目不会放置并产生错误。如果删除该条目,该错误消失,但是我需要该条目。
我已经搜索了堆栈溢出问题,却找不到任何解决问题的方法。
如果您对我有改善问题的建议,请在评论中注明!
Traceback (most recent call last):
File "/Users/Me/PycharmProjects/BT Build 2/GUI.py",line 140,in <module>
getrounds()
File "/Users/Me/PycharmProjects/BT Build 2/GUI.py",line 138,in getrounds
roundgetter.create_window(200,130,window=roundsget)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py",line 2809,in create_window
return self._create('window',args,kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py",line 2771,in _create
return self.tk.getint(self.tk.call(
_tkinter.TclError: bad window path name ".!entry"
import tkinter as tk
def getrounds():
root = tk.Tk()
roundgetter = tk.Canvas(root,width=400,height=300)
roundgetter.pack()
roundsprompt = tk.Label(text="How many rounds would you like to play?")
roundsget = tk.Entry()
def confirmed():
intvar = roundsget.get()
button1 = tk.Button(root,text='Confirm',command=lambda: confirmed())
roundgetter.create_window(200,160,window=button1)
roundgetter.create_window(200,window=roundsget)
roundgetter.create_window(200,100,window=roundsprompt)
root.mainloop()
这是主要代码
import time
import random
import tkinter as tk
seed = random.randint(-2147483647,2147483647)
random.seed(seed)
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class things:
ownedbarrel = 10
ownedrefining = 10
ownedgas = 10
# will be redefined later
Finalscore = 10000
rounds = 16
barrelcost = 40
refiningcost = 10
account = 10000
gasbid = 0
refiningbid = 0
barrelbid = 0
me = tk.Tk()
infoteller = tk.Canvas(me,width=430,height=50)
infoteller.pack()
creator = tk.Label(me,text="Welcome to the Belvedere Trading Oil trading simulation Alpha 1.5.4")
system = tk.Label(me,text="Copyright © 2020 Grant Hutchinson")
infoteller.create_window(215,15,window=creator)
infoteller.create_window(215,40,window=system)
me.update()
infoteller.destroy()
time.sleep(3)
def main():
for x in range(0,things.rounds):
# cost change calculations
costchange = random.randint(-4,4)
refiningcostchange = random.randint(-1,1)
things.refiningcost = things.refiningcost + refiningcostchange
things.barrelcost = things.barrelcost + costchange
things.gascost = things.barrelcost + 10 + costchange * 1.1 + random.randint(-2,2)
# Stablize
if things.barrelcost < 10:
things.barrelcost = things.barrelcost + 3
if things.barrelcost > 70:
things.barrelcost = things.barrelcost - 3
if things.barrelcost < 20:
things.barrelcost = things.barrelcost + 1
if things.barrelcost > 60:
things.barrelcost = things.barrelcost - 1
if things.refiningcost > 12:
things.refiningcost = things.refiningcost - 1
if things.refiningcost < 8:
things.refiningcost = things.refiningcost + 1
# total cost calculations
things.barrelask = things.barrelcost * 1.01
things.barrelbid = things.barrelcost * 0.99
things.barrelask = round(things.barrelask,2)
things.barrelbid = round(things.barrelbid,2)
things.refiningask = things.refiningcost * 1.05
things.refiningbid = things.refiningcost * 0.95
things.refiningask = round(things.refiningask,2)
things.refiningbid = round(things.refiningbid,2)
things.gasask = things.gascost * 1.01
things.gasbid = things.gascost * 0.99
things.gasask = round(things.gasask,2)
things.gasbid = round(things.gasbid,2)
root = tk.Tk()
tellcanv = tk.Canvas(root,width=800,height=500)
tellcanv.pack()
l1 = tk.Label(root,text="Oil ask")
a1 = tk.Label(root,text=things.barrelask)
l2 = tk.Label(root,text="Oil bid")
a2 = tk.Label(root,text=things.barrelbid)
l3 = tk.Label(root,text="Refining ask")
a3 = tk.Label(root,text=things.refiningask)
l4 = tk.Label(root,text="Refining bid")
a4 = tk.Label(root,text=things.refiningbid)
l5 = tk.Label(root,text="Gas ask")
a5 = tk.Label(root,text=things.gasask)
l6 = tk.Label(root,text="Gas bid")
a6 = tk.Label(root,text=things.gasbid)
tellcanv.create_window(400,20,window=l1)
tellcanv.create_window(400,window=a1)
tellcanv.create_window(400,60,window=l2)
tellcanv.create_window(400,80,window=a2)
tellcanv.create_window(400,window=l3)
tellcanv.create_window(400,120,window=a3)
tellcanv.create_window(400,140,window=l4)
tellcanv.create_window(400,window=a4)
tellcanv.create_window(400,180,window=l5)
tellcanv.create_window(400,200,window=a5)
tellcanv.create_window(400,220,window=l6)
tellcanv.create_window(400,240,window=a6)
tellcanv.pack()
root.update()
# Oil system
self = tk.Tk()
oilcanvas = tk.Canvas(self,height=300)
oilcanvas.pack()
self.oilget = tk.Entry(self)
def oilbuy():
intvar = self.oilget.get()
print(intvar)
def oilsell():
intvar = self.oilget.get()
print(intvar)
roundsprompt = tk.Label(self,text="How much oil would you like to trade?")
buyselector = tk.Button(self,text='Buy',command=oilbuy())
sellselector = tk.Button(self,text='Sell',command=oilsell())
oilcanvas.create_window(200,window=roundsprompt)
oilcanvas.create_window(200,150,window=self.oilget)
oilcanvas.create_window(180,window=buyselector)
oilcanvas.create_window(220,window=sellselector)
root.mainloop()
# get rounds
def getrounds():
root = tk.Tk()
roundgetter = tk.Canvas(root,height=300)
roundgetter.pack()
roundsprompt = tk.Label(text="How many rounds would you like to play?")
roundsget = tk.Entry()
def confirmed():
intvar = roundsget.get()
things.rounds = int(intvar)
button1 = tk.Button(root,window=roundsprompt)
root.mainloop()
getrounds()
main()
它应该会出现一个窗口,询问您要打几轮,但不会。
解决方法
我不知道您在代码中做了什么(我看了一下,但是太复杂了),如果您的问题是创建更多的窗口,请看一下我对此发表的这篇文章:how to make more windows with tkinter
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。