从文件中读取行时出错(Python)

时间:2017-10-10 20:11:25

标签: python file filereader

def roll():

    result_Dice = random.randint(1, 6)

def read_dice_lines():

    file2 = open(dice_file_name + '.txt')
    lines = file2.read().split(',')
    file2.close()

    mainMenu.destroy()
    rollDice = tkinter.Tk()
    rollDice.title("Roll That Dice!")

    lblRollOutput = tkinter.Label(rollDice, text="")
    btnRoll = tkinter.Button(rollDice, text="Roll me!", command=roll)

    if diceOutPut == "1":
        lblRollOutput.config(text=lines[0])
    elif diceOutPut == "2":
        lblRollOutput.config(text=lines[1])
    elif diceOutPut == "3":
        lblRollOutput.config(text=lines[2])
    elif diceOutPut == "4":
        lblRollOutput.config(text=lines[3])
    elif diceOutPut == "5":
        lblRollOutput.config(text=lines[4])
    elif diceOutPut == "6":
        lblRollOutput.config(text=lines[5])
    else:
        lblRollOutput.config(text="There has been an error, sorry.")

def createDice():

    mainMenu.destroy()

    global createMenu
    createMenu = tkinter.Tk()
    createMenu.title("Dice Maker.")

    global lblName
    global entName
    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)
    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()
    lblOutcomes.pack()
    entOut1.pack()
    entOut2.pack()
    entOut3.pack()
    entOut4.pack()
    entOut5.pack()
    entOut6.pack()
    btnConfirm.pack()

def createDice2():

    global name
    global nameTxt
    global out1
    global out2
    global out3
    global out4
    global out5
    global out6

    name = entName.get()
    nameTxt = name + ".txt"
    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.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():
  for dice_file_name in glob.glob('*.txt'):
        print(dice_file_name)
        btnDie = tkinter.Button(frameCustomDice, text=os.path.splitext, command=read_dice_lines(dice_file_name)[0])
        btnDie.pack()
        mainMenu.update()
def exit():

    sys.exit()

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)
    btnExit = tkinter.Button(mainMenu, text="Exit", command=exit)

    btnCreateDice.pack()
    btnLoadDice.pack()
    frameCustomDice.pack()
    btnExit.pack()

    mainMenu.mainloop()
mainMenu()

我正在制作一个程序,你可以创建自己的骰子,然后使用它。在我的代码的顶部,我试图将骰子文件中的所有6行放入列表中。当我运行代码并按'加载骰子'时,它会出现此错误:btnDie = tkinter.Button(frameCustomDice,text = os.path.splitext,command = read_dice_lines(dice_file_name)[0]) TypeError:read_dice_lines()需要0个位置参数,但是给出了1,我将如何解决这个问题?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要将该参数添加到要接受的函数中。目前是def read_dice_lines():

将其更改为:

def read_dice_lines(dice_file_name):

正如您在此处command=read_dice_lines(dice_file_name)[0]所说的那样,您将遇到其他问题,因为read_dice_lines不返回任何内容,因此默认为None。然后你试图得到None的第一个elemet [0],它会引发一个错误。因此,您需要删除[0]或返回列表,元组或可索引的内容。