如何在 Python GUI 中从用户输入的数字中使用任意数量的可用文本框
Posted
技术标签:
【中文标题】如何在 Python GUI 中从用户输入的数字中使用任意数量的可用文本框【英文标题】:How to use an arbitrary amount of usable text boxes from a user inputted number in Python GUI 【发布时间】:2013-12-05 06:21:41 【问题描述】:我正在用 Python 编写一个程序,它是一个骰子游戏的评分系统。游戏可以有任意数量的玩家,所以我有一个输入可以让用户说出他们有多少玩家。
我已经能够将标题标签与每个玩家的名字一起打印到 GUI 中。但是,我尝试为每个人打印一个文本框以便可以输入他们的回合分数,这给我带来了麻烦。我尝试运行一个最多玩家数量的 for 循环,并为每个人打印一个文本框。该方法的问题在于它重复使用了我的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()
这真的让我很烦恼。我考虑过连接,但是,在做self.ConcatenatedVarName = Entry(...)
时我不太知道该怎么做。因为,当我连接时,我会使用eval("Player" + CounterInForLoop + "roundScore")
,但 SPE 不喜欢这样做。
任何帮助都会很棒。我真的不想写 50(?) if 语句打印不同数量的文本框if i == NumPlayers
谢谢。
【问题讨论】:
【参考方案1】:没关系,我自己想出来的,这是其他有类似问题的人的解决方案。
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")
就如何打印而言,还有一些其他事情要弄清楚,但这些都是次要的,一旦我尝试,我将能够修复它们。
感谢任何看过它并试图弄清楚的人,即使你没有成功。
【讨论】:
以上是关于如何在 Python GUI 中从用户输入的数字中使用任意数量的可用文本框的主要内容,如果未能解决你的问题,请参考以下文章