def createDice():
mainMenu.destroy()
global createMenu
createMenu = tkinter.Tk()
createMenu.title("Dice Maker.")
global lblName
global entName
global lblPassword
global entPassword
global lblOutcomes
global entOut1
global entOut2
global entOut3
global entOut4
global entOut5
global entOut6
global btnConfirm
lblName = tkinter.Label(createMenu, text="What would you like to call your dice? Dont put .(file type) at the end.")
entName = tkinter.Entry(createMenu)
lblPassword = tkinter.Label(createMenu, text="If you would like a password in order\n to use your dice, enter one.")
entPassword = tkinter.Entry(createMenu)
lblOutcomes = tkinter.Label(createMenu, text="Enter the outcomes for the dice, the bottom one is outcome 6.")
entOut1 = tkinter.Entry(createMenu)
entOut2 = tkinter.Entry(createMenu)
entOut3 = tkinter.Entry(createMenu)
entOut4 = tkinter.Entry(createMenu)
entOut5 = tkinter.Entry(createMenu)
entOut6 = tkinter.Entry(createMenu)
btnConfirm = tkinter.Button(createMenu, text="Confirm", command=createDice2)
lblName.pack()
entName.pack()
lblPassword.pack()
entPassword.pack()
lblOutcomes.pack()
entOut1.pack()
entOut2.pack()
entOut3.pack()
entOut4.pack()
entOut5.pack()
entOut6.pack()
btnConfirm.pack()
def createDice2():
global name
global nameTxt
global password
global out1
global out2
global out3
global out4
global out5
global out6
name = entName.get()
nameTxt = name + ".txt"
password = entPassword.get()
out1u = entOut1.get()
out2u = entOut2.get()
out3u = entOut3.get()
out4u = entOut4.get()
out5u = entOut5.get()
out6u = entOut6.get()
out1 = out1u + "\n"
out2 = out2u + "\n"
out3 = out3u + "\n"
out4 = out4u + "\n"
out5 = out5u + "\n"
out6 = out6u + "\n"
f = open(nameTxt, 'w')
f.write(out1)
f.write(out2)
f.write(out3)
f.write(out4)
f.write(out5)
f.write(out6)
f.write(password)
f.close()
createMenu.destroy()
restartpls = tkinter.Tk()
restartpls.title("Restart this program")
lblrestart = tkinter.Label(restartpls, text="Please restart this program, this is needed so that\n the program can see that you have created\n a new dice.")
lblrestart.pack()
restartpls.mainloop()
sys.exit()
def loadDice():
#see the other .txt files around the python file and make a button for each of them.
#other code i will do.
def mainMenu():
global mainMenu
mainMenu = tkinter.Tk()
mainMenu.title("Menu")
btnCreateDice = tkinter.Button(mainMenu, text="Create Dice", command=createDice)
btnLoadDice = tkinter.Button(mainMenu, text="Load Dice")
btnExit = tkinter.Button(mainMenu, text="Exit", command=exit)
btnCreateDice.pack()
btnLoadDice.pack()
btnExit.pack()
mainMenu.mainloop()
mainMenu()
当您运行此代码时,它会要求您创建骰子,退出或加载骰子。我编写了创建骰子和退出按钮程序。我无法弄清楚如何让python为与它相同的文件夹中的每个.txt文件创建一个按钮并将其放在按钮中。然后当按下一个特定按钮时,它会将前6行代码放入骰子的输出,将第7行放入密码。
如何让python为与其相同的文件夹中的每个.txt文件创建一个按钮并将其放在按钮中
我将此代码放在def loadDice():底部。
答案 0 :(得分:0)
这是一个基本的例子,说明如何使用全局变量并且不保留按钮对象(因为它们仅在本地定义)。我已将按钮放在一个框架中,这样您就可以在加载骰子按钮之后但在退出按钮之前打包按钮。为了简单起见,我已将其设为全局,因为mainMenu
已经是全局的,但我还提供了一个注释掉的示例,说明如何使用functools.partial
将参数传递给按钮命令。
import glob
import functools
import os
... the rest of your code
def loadDice():
for dice_file_name in glob.glob('*.txt'):
print(dice_file_name)
btnDie = tkinter.Button(frameCustomDice, text=os.path.splitext(dice_file_name)[0])
btnDie.pack()
mainMenu.update()
def mainMenu():
global mainMenu
mainMenu = tkinter.Tk()
mainMenu.title("Menu")
global frameCustomDice
frameCustomDice = tkinter.Frame(height=2, bd=1, relief=tkinter.SUNKEN)
btnCreateDice = tkinter.Button(mainMenu, text="Create Dice", command=createDice)
btnLoadDice = tkinter.Button(mainMenu, text="Load Dice", command=loadDice)
# btnLoadDice = tkinter.Button(mainMenu, text="Load Dice", command=functools.partial(loadDice, mainMenu, frameCustomDice))
btnExit = tkinter.Button(mainMenu, text="Exit", command=exit)
btnCreateDice.pack()
btnLoadDice.pack()
frameCustomDice.pack()
btnExit.pack()
mainMenu.mainloop()
mainMenu()
从这一点来看,需要考虑一些要点:
global
变量,方法是将必要的变量作为参数传递给命令。functools.partial
为loadDice
中创建的每个按钮提供自定义参数化命令,方法是将文件名传递给某个函数(比如rollDice
),或者解析概率在loadDice
中文件的两侧,并将这些概率作为参数传递给rollDice
。loadButton
两次,它将再次加载按钮。我想你可以先从框架中删除所有按钮,或保存一些关于加载按钮的数据,并在加载新按钮之前检查一下。