我是python的初学者,我正在尝试创建一个类似记分板的GUI。我根据Combobox选择通过按钮修改了我的统计数据。小部件之后有4个非常小的功能'代码,第一个按预期工作,另外3个相似,但它们仅影响3个最后标签而不是Combobox选择标签。我在那里想念什么?
from Tkinter import *
import ttk
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
self.master = master
self.box_list = []
self.labels = []
self.radiobuttons = []
self.winlabels = []
self.loselabels = []
self.drawlabels = []
self.create_widgets()
self.grid()
def create_widgets(self):
for i in range(9):
box = ttk.Combobox(self, state="readonly")
box.grid(row=1+i, column=2)
box["values"] = ("team A", "team B", "team C", "team D", "team E", "team F", "team G", "team H", "team I")
box.current(i)
self.box_list.append(box)
box.bind("<<ComboboxSelected>>")
for i in range(9):
sv = 1
selection = i
radiobutton = Radiobutton(self, value=selection, text=selection+1, variable=sv,
command=lambda i=i: self.rcr(i))
radiobutton.grid(row=1+i, column=0)
self.radiobuttons.append(radiobutton)
self.headings = ("Classes", "Win Lose Draw", "Declare Result!")
for i in range(3):
label = Label(self, text=self.headings, relief="sunken")
label["text"] = self.headings[i]
label.grid(row=0, column=i*3+1, columnspan=3, sticky=E+W+N+S)
for i in range(9):
stats = Label(self, relief="sunken")
stats["text"] = 0
stats.grid(row=1+i, column=4, sticky=E+W+N+S)
self.winlabels.append(stats)
for i in range(9):
stats = Label(self, relief="sunken")
stats["text"] = 0
stats.grid(row=1+i, column=5, sticky=E+W+N+S)
self.loselabels.append(stats)
for i in range(9):
stats = Label(self, relief="sunken")
stats["text"] = 0
stats.grid(row=1+i, column=6, sticky=E+W+N+S)
self.drawlabels.append(stats)
buttonwin = Button(self, text="Win", bg="green", command=lambda i=i: self.win(i))
buttonwin.grid(row=1, column=7, pady=1, padx=1, rowspan=3, columnspan=3, sticky=E+W+N+S)
buttonlose = Button(self, text="Lose", bg="red", command=lambda i=i: self.lose(i))
buttonlose.grid(row=4, column=7, pady=1, padx=1, rowspan=3, columnspan=3, sticky=E+W+N+S)
buttondraw = Button(self, text="Draw", bg="white", command=lambda i=i: self.draw(i))
buttondraw.grid(row=7, column=7, pady=1, padx=1, rowspan=3, columnspan=3, sticky=E+W+N+S)
def rcr(self, i):
tr = self.radiobuttons[i]["text"]
print tr
def win(self, i):
self.winlabels[i]["text"] += 1
def lose(self, i):
self.loselabels[i]["text"] += 1
def draw(self,i):
self.drawlabels[i]["text"] += 1
root = Tk()
root.title("Stats Tracker")
root.geometry("500x250")
app = Application(root)
root.mainloop()
答案 0 :(得分:1)
对于您的单选按钮,您应该使用类属性(我的代码中的RBval)作为变量来控制所选值。您不需要将所有单选按钮存储在变量中。
然后,在你的胜利,失败,绘制功能,你不需要输入所选的单选按钮的数量。你只需读取RBval的值。在你的代码中,我总是等于9,因为它是你的forloop中i的最后一个值。
代码中可能存在许多问题,但这是代码的工作版本:
from Tkinter import *
import ttk
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
self.master = master
self.box_list = []
self.labels = []
self.radiobuttons = []
self.winlabels = []
self.loselabels = []
self.drawlabels = []
self.create_widgets()
self.grid()
def create_widgets(self):
for i in range(9):
box = ttk.Combobox(self, state="readonly")
box.grid(row=1+i, column=2)
box["values"] = ("team A", "team B", "team C", "team D", "team E", "team F", "team G", "team H", "team I")
box.current(i)
self.box_list.append(box)
box.bind("<<ComboboxSelected>>")
self.RBval = IntVar()
for i in range(9):
selection = i
radiobutton = Radiobutton(self, value=selection, text=selection+1, variable=self.RBval)
radiobutton.grid(row=1+i, column=0)
self.radiobuttons.append(radiobutton)
self.headings = ("Classes", "Win Lose Draw", "Declare Result!")
for i in range(3):
label = Label(self, text=self.headings, relief="sunken")
label["text"] = self.headings[i]
label.grid(row=0, column=i*3+1, columnspan=3, sticky=E+W+N+S)
for i in range(9):
stats = Label(self, relief="sunken")
stats["text"] = 0
stats.grid(row=1+i, column=4, sticky=E+W+N+S)
self.winlabels.append(stats)
for i in range(9):
stats = Label(self, relief="sunken")
stats["text"] = 0
stats.grid(row=1+i, column=5, sticky=E+W+N+S)
self.loselabels.append(stats)
for i in range(9):
stats = Label(self, relief="sunken")
stats["text"] = 0
stats.grid(row=1+i, column=6, sticky=E+W+N+S)
self.drawlabels.append(stats)
buttonwin = Button(self, text="Win", bg="green", command=self.win)
buttonwin.grid(row=1, column=7, pady=1, padx=1, rowspan=3, columnspan=3, sticky=E+W+N+S)
buttonlose = Button(self, text="Lose", bg="red", command=self.lose)
buttonlose.grid(row=4, column=7, pady=1, padx=1, rowspan=3, columnspan=3, sticky=E+W+N+S)
buttondraw = Button(self, text="Draw", bg="white", command=self.draw)
buttondraw.grid(row=7, column=7, pady=1, padx=1, rowspan=3, columnspan=3, sticky=E+W+N+S)
def win(self):
i = self.RBval.get()
self.winlabels[i]["text"] += 1
def lose(self):
i = self.RBval.get()
self.loselabels[i]["text"] += 1
def draw(self):
i = self.RBval.get()
self.drawlabels[i]["text"] += 1
root = Tk()
root.title("Stats Tracker")
root.geometry("500x250")
app = Application(root)
root.mainloop()