Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python GUI中用户输入的数字中的任意数量的可用文本框_Python_User Interface_Textbox_Concatenation - Fatal编程技术网

如何使用Python GUI中用户输入的数字中的任意数量的可用文本框

如何使用Python GUI中用户输入的数字中的任意数量的可用文本框,python,user-interface,textbox,concatenation,Python,User Interface,Textbox,Concatenation,我正在用Python编写一个程序,这是一个骰子游戏的计分系统。这个游戏可以有任意数量的玩家,所以我有一个输入,允许用户说出他们有多少玩家 我已经能够将标题标签打印到每个球员姓名的GUI中。然而,我试图为每个人打印一个文本框,以便输入他们的总分,这给我带来了麻烦。我试着运行一个for循环,最大化玩家数量,并为每个人打印一个文本框。该方法的问题是,它正在重用myself.PlayerXroundScore,因此只有最后创建的文本框可用 这是我的代码,我已经尽可能地对其进行注释,以使其更易于阅读 #A

我正在用Python编写一个程序,这是一个骰子游戏的计分系统。这个游戏可以有任意数量的玩家,所以我有一个输入,允许用户说出他们有多少玩家

我已经能够将标题标签打印到每个球员姓名的GUI中。然而,我试图为每个人打印一个文本框,以便输入他们的总分,这给我带来了麻烦。我试着运行一个for循环,最大化玩家数量,并为每个人打印一个文本框。该方法的问题是,它正在重用my
self.PlayerXroundScore
,因此只有最后创建的文本框可用

这是我的代码,我已经尽可能地对其进行注释,以使其更易于阅读

#Allows user to input total number of players
NumPlayers = input("How many players? ")

#Creates a list that is the number of players long
NameList = [0]*NumPlayers

#Allows for input of each Players name
#and stores those names in the list NameList
for i in range(0,NumPlayers):

x = raw_input("Player %d Name? " %(i+1))

NameList[i] = x

#creates the GUI
from Tkinter import *
from tkMessageBox import *

class App(Tk):
   def __init__(self):
       Tk.__init__(self)

       self.Title = ("10,000 scorekeeping")
       self.Header = Label(self, text = "Welcome to 10,000 scoring Module, Have Fun!!", font = ("helvetica", "20", "bold")).grid(row = 0, column = 0, columnspan = (NumPlayers * 3))
       for NameCount in range(1,(NumPlayers+1)):

           #Allows me to create the names as column headers
           self.PlayerName = Label(self, text = "%s" %NameList[NameCount - 1],font = ("helvetica","12","bold")).grid(row = 1, column = ((2 * NameCount)))


           #This if just makes things more aesthetically pleasing, not relevant to my question
           if NameCount < (NumPlayers):

                 self.PlayerName = Label(self, text = "|",font = ("helvetica","12","bold")).grid(row = 1, column = ((2 * NameCount + 1)))


           #This is my problem
           #It succesffully prints the correct number of text boxes
           #however upon button click which calls the vals in each text box
           #only the last created box is useful
           #because that is the box corresponding to PlayerXroundScore
           self.PlayerXroundScore = Entry(self, width = 4)
           self.PlayerXroundScore.grid(row = 2, column = (2 * NameCount))
           self.PlayerXroundScore.insert(0, "0000")

       self.NextRound = Button(self, text = "Next round", command = self.CalcRoundTotals)
       self.NextRound.grid(row = 1, column = 0)

   #This is not completed yet, because I wanted to make sure this is the best way to do it before putting in the time
   #Its obviously doing erroneous things but that will change, 
   #I will encounter the same problem in quite a few different places 
   #but if it can be figured out this once, I can incorporate it elsewhere    
   def CalcRoundTotals(self):

       print x

if __name__ == "__main__":        
    a = App()
    a.mainloop()
#允许用户输入玩家总数
NumPlayers=输入(“有多少玩家?”)
#创建一个列表,该列表是长玩家的数量
名称列表=[0]*NumPlayers
#允许输入每个玩家的名字
#并将这些名称存储在列表名称列表中
对于范围内的i(0,NumPlayers):
x=原始输入(“玩家%d姓名?”%(i+1))
名称列表[i]=x
#创建GUI
从Tkinter进口*
从tkMessageBox导入*
类应用程序(Tk):
定义初始化(自):
Tk.\uuuuuu初始(自我)
self.Title=(“10000记分”)
self.Header=Label(self,text=“欢迎来到10000评分模块,玩得开心!!”,font=(“helvetica”,“20”,“bold”)).grid(行=0,列=0,列span=(numlayers*3))
对于范围(1,(numlayers+1))中的名称计数:
#允许我将名称创建为列标题
self.PlayerName=Label(self,text=“%s”%NameList[NameCount-1],font=(“helvetica”,“12”,“bold”))。grid(行=1,列=((2*NameCount)))
#这只会让事情更加美观,与我的问题无关
如果名称计数<(NumPlayers):
self.PlayerName=Label(self,text=“|”,font=(“helvetica”,“12”,“bold”)).grid(行=1,列=((2*NameCount+1)))
#这是我的问题
#它成功地打印出正确数量的文本框
#但是,单击按钮后,会调用每个文本框中的VAL
#只有最后创建的框才有用
#因为这是对应于PlayerXroundScore的框
self.PlayerXroundScore=条目(self,宽度=4)
self.PlayerXroundScore.grid(行=2,列=(2*NameCount))
self.PlayerXroundScore.insert(0,“0000”)
self.nextrund=按钮(self,text=“下一轮”,command=self.CalcRoundTotals)
self.NextRound.grid(行=1,列=0)
#这还没有完成,因为我想在投入时间之前确保这是最好的方法
#它显然做了错误的事情,但这会改变,
#我会在很多不同的地方遇到同样的问题
#但如果这一次可以解决,我可以把它合并到其他地方
def CalcRoundTotals(自身):
打印x
如果名称=“\uuuuu main\uuuuuuuu”:
a=应用程序()
a、 mainloop()
这真让我恼火。但是,当执行
self.ConcatenatedVarName=Entry(…)
时,我考虑过连接,但我不太知道怎么做。因为,当我连接时,我会
eval(“Player”+CounterInForLoop+“roundScore”)
但SPE不喜欢这样做

任何帮助都会很好。如果I==NumPlayers


谢谢。

别管了,我自己解决了,这是其他有类似问题的人的解决方案

self.PlayerTextBox = []
for NameCount in range(1,(NumPlayers + 1)):
      self.PlayerTextBox.append(Entry(self, width = 4))
      self.PlayerTextBox[NameCount - 1].grid(row = 2, column =   
                                       (2 * NameCount))
      self.PlayerTextBox[NameCount - 1].insert(0, "   0")
至于如何打印东西,我还有一些其他的事情要解决,但这些都是次要的,一旦我尝试,我就能解决它们

感谢所有看过它并试图找出答案的人,即使你失败了