繁体   English   中英

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

[英]Error with reading lines from a file (Python)

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个,我该如何解决? 任何帮助都感激不尽。

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

更改为:

def read_dice_lines(dice_file_name):

请注意,在此处调用command=read_dice_lines(dice_file_name)[0]您将遇到其他问题,因为read_dice_lines不返回任何内容,因此默认为None 然后,您尝试获取无的第一个元素[0] ,这将引发错误。 因此,您将需要删除[0]或返回列表,元组或可索引的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM