簡體   English   中英

使用Tkinter將值傳遞給python腳本

[英]Pass values to python script with Tkinter

因此,我編寫了一個使用PIL庫格式化傳遞給它的png的python腳本。 我想使腳本更加用戶友好,環顧四周后,我看到了Tkinter庫,它看起來很完美。 基本上,腳本具有五個變量,我需要將其傳遞給腳本才能運行。 我目前正在使用raw_input()函數向用戶詢問以下變量:

path_to_png
title
subtitle
source
sample_size

收到腳本后,腳本將運行並導出格式化的png。 如下面的圖片所示,我已經使用Tkinter構建了這些基本輸入,但是我不知道如何將輸入的文本值和文件路徑從select png按鈕傳遞到它們各自的變量。

在此處輸入圖片說明

from Tkinter import *
from tkFileDialog import askopenfilename
from tkMessageBox import *

app = Tk()
app.title("Picture Formatting")
app.geometry('500x350+200+200')

# 
def callback():
    chart_path = askopenfilename()
    return

def title_data():
    title_data = chart_title
    return

errmsg = 'Error!'
browse_botton = Button(app, text="Choose png", width=15, command=callback)
browse_botton.pack(side='top', padx=15, pady=15)

# Get chart data
chart_title = StringVar()
title = Entry(app, textvariable = chart_title)
title.pack(padx=15, pady=15)

chart_subtitle = StringVar()
subtitle = Entry(app, textvariable = chart_subtitle)
subtitle.pack(padx=15, pady=15)

chart_source = StringVar()
source = Entry(app, textvariable = chart_source)
source.pack(padx=15, pady=15)

chart_sample_size = IntVar()
sample_size = Entry(app, textvariable = chart_sample_size)
sample_size.pack(padx=15, pady=15)

submit_button = Button(app, text="Submit", width=15)
submit_button.pack(side='bottom', padx=15, pady=15)

app.mainloop()

我已經嘗試了您的代碼,並添加了以下幾行:

from Tkinter import *
from tkFileDialog import askopenfilename
from tkMessageBox import *

app = Tk()
app.title("Picture Formatting")
app.geometry('500x350+200+200')

# 
def callback():
    global chart_path
    chart_path = askopenfilename()
    return

def title_data():
    title_data = chart_title
    return

def calculate():
    chart_title = title.get()
    chart_subtitle = subtitle.get()
    chart_source = source.get()
    chart_sample_size = sample_size.get()

    print "chart_path : ", chart_path
    print "chart_title : ", chart_title
    print "chart_subtitle : ", chart_subtitle
    print "chart_source : ", chart_source
    print "chart_sample_size : ", chart_sample_size

    #Call your functions here

    return

errmsg = 'Error!'

# Get chart data

chart_path = ''
browse_botton = Button(app, text="Choose png", width=15, command=callback)
browse_botton.pack(side='top', padx=15, pady=15)

chart_title = StringVar()
title = Entry(app, textvariable = chart_title)
title.pack(padx=15, pady=15)

chart_subtitle = StringVar()
subtitle = Entry(app, textvariable = chart_subtitle)
subtitle.pack(padx=15, pady=15)

chart_source = StringVar()
source = Entry(app, textvariable = chart_source)
source.pack(padx=15, pady=15)

chart_sample_size = IntVar()
sample_size = Entry(app, textvariable = chart_sample_size)
sample_size.pack(padx=15, pady=15)

submit_button = Button(app, text="Submit", width=15, command=calculate)
submit_button.pack(side='bottom', padx=15, pady=15)

app.mainloop()

我認為您要問的問題是如何獲取條目小部件中的文本值以及如何從askopenfilename()函數獲取路徑文本。 您可以使用Entry.get()方法來獲取某些條目小部件中的文本值。

您可以只使用str = askopenfilename()來獲取路徑的文本值。 但是因為這行代碼是在函數中編寫的,所以您需要聲明它是全局變量或創建一個包含它們的類,否則解釋器將認為該變量是局部變量,並且不會傳遞給該函數我添加的calculate()

由於您沒有使用類來包含變量,因此我也將變量用作全局變量。 這不是一個好的設計。 您可以考慮創建一個類。

為了從條目窗口小部件接收值,您需要使用get()函數。 get()函數將返回條目窗口小部件中的任何內容。 例如,在您的情況下: response = sample_size.get()會將變量response設置為0。有關條目小部件的更多文檔,請參response = sample_size.get() 頁面

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM