为了账号安全,请及时绑定邮箱和手机立即绑定

如何从命令 tkinter 中获取价值

如何从命令 tkinter 中获取价值

皈依舞 2021-10-19 15:07:07
这可能是一个新手问题,但我在弄清楚如何从函数中获取值以用作另一个函数的输入时遇到问题。我还想了解一些有关代码组织的技巧。在此示例中,我试图获取要在函数 processFile 中使用的 filePath 和 outPut Folder 路径。谢谢代码:    from tkinter import *from tkinter import ttkfrom tkinter.filedialog import askopenfilenamefrom tkinter.filedialog import askdirectoryimport PyPDF2from PyPDF2 import PdfFileWriter, PdfFileReaderversion = "v0.0.01"root = Tk()def window (main):    main.title("File Opener " + version)    width = main.winfo_width()    main.geometry('500x200')numero = str(1)def OpenFile():    fileName = askopenfilename(initialdir="C:/",                            filetypes =(("PDF File", "*.pdf"),("All Files","*.*")),                            title = "Select PDF File",                           )    labelName = Label(text="File Path: " + fileName)    labelName.pack()    print(fileName)    return fileNamedef outputFolder():                                         #Bug, label keeps adding paths    outPath = askdirectory()    labelName2 = Label(text="Output Folder: " + outPath)    labelName2.pack()    print(outPath)    return outPathdef processFile(inFile,outFolder):   ''' print(fileName) get input name and output variables    print(outPath)'''label = ttk.Label(root, text ="",foreground="black",font=("Helvetica", 16))label.pack()#Button Open-----------button1 = Button (text = "Open File", command = OpenFile)button1.pack()#Button Start---------buttonStart = Button (text = "Start Process", command = processFile)#give as parameter inputFile and linkbuttonStart.place(height=50, width=100)#Button Open-----------button3 = Button (text = "Output Folder", command = outputFolder)button3.pack()#Menu Bar ----------------menu = Menu(root)root.config(menu=menu)file = Menu(menu)file.add_command(label = 'Open', command = OpenFile)file.add_command(label = 'Exit', command = lambda:exit())menu.add_cascade(label = 'File', menu = file)window(root)root.mainloop()
查看完整描述

2 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

如果您在变量中使用该函数,则该函数的返回结果将存储在该变量中。


def Function1(x):

    return x*2


var1 = Function1(5)

然后 var1 = 10。


但是关于您的按钮,您是想从用户那里获取在按下按钮时保存的输入还是希望按钮发送一个设定值?


查看完整回答
反对 回复 2021-10-19
?
狐的传说

TA贡献1804条经验 获得超3个赞

from tkinter import *

from tkinter import ttk

from tkinter.filedialog import askopenfilename

from tkinter.filedialog import askdirectory

import PyPDF2

from PyPDF2 import PdfFileWriter, PdfFileReader


version = "v0.0.01"



root = Tk()





def window (main):


    main.title("File Opener " + version)

    width = main.winfo_width()

    main.geometry('500x200')

    main.configure(background = 'black')


numero = str(1)


#openFile

def OpenFile():

    global fileName

    fileName = askopenfilename(initialdir="C:/",

                            filetypes =(("PDF File", "*.pdf"),("All Files","*.*")),

                            title = "Select PDF File",

                           )


    labelName = Label(text="File Path: " + fileName)

    labelName.pack()

    print(fileName)

    return str(fileName)


#getOutputPath


def outputFolder():                                         #Bug, label keeps adding paths

    outPath = askdirectory()


    labelName2 = Label(text="Output Folder: " + outPath)

    labelName2.pack()

    print(outPath)

    return outPath


#testFunct


def printFilename(inArg):

    print(inArg)

    x = OpenFile()

    print (OpenFile())





lambda: printFilename(fileName)


#processRealDeal


def processFile(): #THIS IS THE MAIN FUNC


   ''' print(fileName) get input name and output variables

    print(outPath)'''



print (OpenFile)


label = ttk.Label(root, text ="",foreground="black",font=("Helvetica", 16))

label.pack()



#Button Open-----------


button1 = Button (text = "Open File", command = OpenFile)

button1.pack()



#Button Start---------

buttonStart = Button (text = "Start Process", command = lambda: printFilename("Hello World!!!"))#give as parameter inputFile and link

buttonStart.place(height=50, width=100)


#Button Open-----------


button3 = Button (text = "Output Folder", command = outputFolder)

button3.pack()



#Menu Bar ----------------


menu = Menu(root)

root.config(menu=menu)


file = Menu(menu)



file.add_command(label = 'Open', command = OpenFile)

file.add_command(label = 'Exit', command = lambda:exit())


menu.add_cascade(label = 'File', menu = file)



window(root)

root.mainloop()


查看完整回答
反对 回复 2021-10-19
  • 2 回答
  • 0 关注
  • 176 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号