如何从命令tkinter

时间:2019-01-24 09:38:05

标签: python optimization tkinter

这可能是一个新手问题,但我在弄清楚如何从一个函数中获取值,以用作另一个的输入时遇到了问题。 我还想了解一些有关代码组织的技巧。 在此示例中,我试图获取在processFile函数中使用的filePath和outPut文件夹路径。

谢谢

代码:

    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')

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 fileName

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

    labelName2 = Label(text="Output Folder: " + outPath)
    labelName2.pack()
    print(outPath)
    return outPath



def 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 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()

3 个答案:

答案 0 :(得分:1)

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

def Function1(x):
    return x*2

var1 = Function1(5)

然后var1 = 10。

但是关于您的按钮,您是否要接受用户的输入,该输入将在按下按钮时保存?或者您希望按钮发送设定值?

答案 1 :(得分:0)

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()

答案 2 :(得分:0)

我看不到您已经在函数外部声明了全局fileName variabel。

我做到了

Request.Properties["totalResults"]

然后测试功能将打印所选文件。因此,全局变量现在包含正确的值。

所以只需在顶部声明它,就可以检索到值