无法在Main()内调用函数

2024-10-02 02:40:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个初学者,目前正在进行一个个人项目,使用Tkinter在Python2上开发一个小型应用程序。但是,我在调用我创建的函数时遇到了问题。 1。知道Python上的Tkinter是基于事件的,因此我创建了一个按钮来上传一个特定的文件,然后将文件中的值存储到一个列表变量中。 2。之后,我使用另一个按钮尝试调用print函数来打印List变量中存储的值中的值。你知道吗

希望有人能帮我。你知道吗

def uploadFile():
   openFile = tkFileDialog.askopenfilename(filetypes = (("",".csv"),("All 
                 files", "*.*")))

   with open(openFile) as file:
     reader = csv.reader(file)
     xUF = [row for row in reader]
   return xUF

def printUF():
   for row in xUF:
       print row
       count=0
       if count>5:
          break
       count += 1

def main():
   l1=tk.Label(root,text="Upload File")
   l1.pack()
   l1.place(x=55,y=170)
   #buttons/placements for GUI
   b1=tk.Button(root,text="Browse", command=uploadFile)
   b1.pack()
   b1.place(x=100,y=200)

   b2=tk.Button(root,text="Print", command=printUF())
   b2.pack()


   root.title("HELLO")
   root.geometry("500x500")
   root.mainloop()


if __name__ == '__main__':
   main()

Tags: 函数textl1formaintkinterdefcount
2条回答

我不太清楚你的错误是什么。但我怀疑是这样的:

当我认为您只需要command=printUF时,您正在将command=printUF()传递给Button类,因为Button可能希望在函数被命中时调用它。现在,您正在将printUF的结果而不是函数本身传递给Button。线条应如下所示:

b2=tk.Button(root,text="Print", command=printUF)

第行缺少括号

def printUF():
   for row in xUF:
    print (row) < -
   count=0
   if count>5:
      break
   count += 1

相关问题 更多 >

    热门问题