【发布时间】:2016-01-15 06:48:32
【问题描述】:
在 python 中使用 TKinter 遇到一些麻烦。在慢慢构建一个具有单个主窗口和 4 个选项卡(笔记本)的简单程序时,我在确定如何将文件名传递给需要它的函数时遇到了麻烦。
我有一个按钮可以打开文件对话框并允许用户选择文件并(希望)检索文件名。我有另一个按钮设置,它将(希望)触发一个进程,将所选文件的名称作为参数传递给命令中调用的方法。现在,我可以触发打开文件对话框,并将文件名打印到控制台,但我不知道如何将此文件名传递给 delineate 命令(这最终将触发一个不同的脚本,其中文件名作为输入脚本)。我进行了设置,以便在触发“Delineate”按钮时,它会在窗口上显示“Delineating....”。我的目标是能够显示“Delineating X”,其中 X 是打开文件的名称,并将此名称传递给另一个脚本。如果我可以让它正确显示“Delineating X”,这意味着文件名已经通过,我想我可以做剩下的......
代码如下:
from Tkinter import *
import ttk
import tkFileDialog
import tkMessageBox
import Tkconstants
class Unified_Tool(ttk.Frame):
# =============================================================================
# Main setup options
def __init__(self, isapp=True, name='unified_tool'):
ttk.Frame.__init__(self, name=name)
self.master.title("Unified Tool")
self.pack(expand=Y, fill=BOTH)
self.isapp = isapp
self._create_widgets()
file_name = StringVar()
def _create_widgets(self):
self._create_panels()
def _create_panels(self):
panel = Frame(self, name='panel')
panel.pack(side=TOP, fill=BOTH, expand=Y)
# create the notebook
nb = ttk.Notebook(panel, name='notebook_panel')
nb.pack(fill=BOTH, expand=Y, padx=2, pady=3)
self._create_ca_del_tab(nb)
#self._create_traversal_tab(nb)
#self._create_tot_tab(nb)
#self._create_zcc_tab(nb)
# =============================================================================
# Delineation tab
def load_file(self):
file_name = tkFileDialog.askopenfilename(filetypes=([('All files', '*.*'),
('Text files', '*.txt'),
('CSV files', '*.csv')]))
print file_name
# =============================================================================
# Delineation tab
def _create_ca_del_tab(self, nb):
# variable to store the filename
del_filename = StringVar()
# frame to hold content
frame = ttk.Frame(nb, name='ca_delineation')
# widgets to be displayed on 'Description' tab
msg = ["For delineating the catchment area of a point or collection of points. In "
"the file selection box to the left, select the input file containing the points "
"for catchment area delineation."]
lbl_intro = ttk.Label(frame, wraplength='4i', justify=LEFT, anchor=N,
text=''.join(msg))
# button for selecting the input file
btn_del_select_file = ttk.Button(frame, text="Browse", command=self.load_file, width=10)
# button for triggering the task
btn_del = ttk.Button(frame, text='Delineate!', underline=0,
command=lambda v=del_filename: self._delineate(del_filename))
# label that displays the input file name
lbl_del = ttk.Label(frame, textvariable=del_filename, name='delineate')
# position and set resize behaviour
lbl_intro.grid(row=0, column=0, columnspan=2, sticky='new', pady=5)
lbl_del.grid(row=1, column=1, pady=(2,4))
btn_del_select_file.grid(row=1, column=0, pady=(2,4))
btn_del.grid(row=2, column=0, pady=(2,4))
frame.rowconfigure(1, weight=1)
frame.columnconfigure((0,1), weight=1, uniform=1)
# add to notebook (underline = index for short-cut character)
nb.add(frame, text='CA Delineation', underline=0, padding=2)
def _delineate(self, v):
v.set('Delineating....')
self.update()
【问题讨论】:
-
请更改您的
class method。检查此 PDF google.com.tr/… 你为什么要让事情变得复杂?核子控制系统? -
我正在构建一个带有四个选项卡的窗口 - 现在只处理第一个选项卡。对于一个有四个不同标签的美国来说,这似乎很复杂,所有标签都有它们的按钮/标签/操作?