请原谅我,如果这是一种严重损害的做事方式,但我对一般的发展都不熟悉。
我正在尝试使用tkinter创建一个带有多个按钮的窗口,每个按钮上都有一个玩家的名字,使用一个名为main()的类。
然后我希望能够在应用程序中稍后按下的按钮上使用该名称,因此我想将其传递回main()。所以,如果我点击Annie按钮,我想打开一个新的窗口,后来称为'Annie for Annie',我假设值'Annie'需要传回主函数。
我的主要代码:
<imports appear here>
def main():
players = ['Annie','Benny','Carrie','Donny']
winHome = playerWindow(root,players)
print("In main() : " + winHome.selected)
root.mainloop()
if __name__ == "__main__":
main()
我的班级代码:
<imports appear here>
root=Tk()
class playerWindow():
def selectPlayer(self,selname):
self.selected = selname
print("In class: " + self.selected)
def __init__(self, master, players=[]):
colours = ['red','green','orange','white','yellow','blue']
self.selected = ""
r = 0
for p in players:
randcol = random.randint(0,len(colours))-1
if colours[randcol] in ('blue','green'):
fgcol='white'
else:
fgcol='black'
playername = delplayer = p
playername = Button(root, text=playername, bg=colours[randcol], fg=fgcol, width=15, command=lambda name = playername:self.selectPlayer(name)).grid(row=r,column=0)
s = ttk.Separator(root, orient=VERTICAL)
delplayer = Button(root, text='Del', bg='grey', fg='red', width=5, command=lambda name = delplayer: print("Delete Player %s" % name)).grid(row=r,column=1)
r = r + 1
Button(root, text="New Player", fg="black", command=lambda: print("New Player Functionality"), width=15).grid(row = len(players)+3,column=0)
Button(root, text="QUIT", fg="red", command=root.destroy, width=15).grid(row = len(players)+3,column=1)
正在发生的是窗口被创建,main()中的下一行被运行(我添加的print语句),这是空的,显然主要是继续。当我按下按钮时,会调用sleectPlayer函数并起作用。
不知怎的,我需要将值返回到main()以继续使用该值进行下一个任务,但是我似乎无法正确构建问题以获得我需要的答案。
非常感谢任何帮助。
我正在使用Python 3.5.1
答案 0 :(得分:2)
您已经在访问它了。我个人不喜欢返回主函数,而是建议创建一个顶级类来链接回来。这应该有助于使事情变得更好。
import tkinter as tk
from tkinter import ttk
import random
class PlayerWindow():
def __init__(self, master, parent, players=[]):
self._parent = parent
colours = ['red','green','orange','white','yellow','blue']
self.selected = ""
r = 0
for p in players:
randcol = random.randint(0,len(colours))-1
if colours[randcol] in ('blue','green'):
fgcol='white'
else:
fgcol='black'
playername = delplayer = p
playername = tk.Button(master, text=playername, bg=colours[randcol], \
fg=fgcol, width=15, command=lambda name = \
playername:self.selectPlayer(name)).grid(row=r,column=0)
s = ttk.Separator(master, orient=tk.VERTICAL)
delplayer = tk.Button(master, text='Del', bg='grey', fg='red', \
width=5, command=lambda name = delplayer: \
print("Delete Player %s" % name)).grid(row=r,column=1)
r = r + 1
tk.Button(master, text="New Player", fg="black", command=lambda: \
print("New Player Functionality"), width=15).\
grid(row = len(players)+3,column=0)
tk.Button(master, text="QUIT", fg="red", command=self._parent.close,
width=15).grid(row = len(players)+3,column=1)
def selectPlayer(self, selname):
self.selected = selname
print("In class: " + self.selected)
self._parent.hello() # call hello function of top-level, links back
class MyApplication(object):
def __init__(self, master):
self._master = master
players = ['Annie','Benny','Carrie','Donny']
self._player_window = PlayerWindow(master, self, players)
print("In main() : " + self._player_window.selected)
def hello(self):
name = self._player_window.selected
print("Hello, %s" % name)
def close(self):
# any other clean-up
self._master.destroy()
def main():
root = tk.Tk()
app = MyApplication(root)
root.mainloop()
if __name__ == "__main__":
main()