【发布时间】:2018-04-12 16:12:54
【问题描述】:
这是我尝试做的一个非常简化的版本。
import Tkinter as tk
root = tk.Tk()
n = 1
def show1():
print var.get()
def add1():
global n
l = 'Option %d' % n
n += 1
op['menu'].add_command(label=l,command=tk._setit(var,l))
var.set(l)
print 'added %s' % l
var = tk.StringVar()
op = tk.OptionMenu(root,var,[], command=show1)
op.pack()
tk.Button(root,text='add 1', command=add1).pack()
tk.Button(root,text='show', command=show1).pack()
root.mainloop()
“添加 1”按钮成功地将选项添加到选项菜单。 单击选项不会调用 show1 例程,您必须单击“显示”按钮。
-------原始问题-----
我正在尝试读入一些选定的文件并保存每个文件的信息。
当我在文件中读取它们时,基本名称存储在选项列表中。这似乎奏效了。
我想在单击选项菜单中的选项时显示保存的信息。那是行不通的。
下面的代码是将名称添加到选项列表中,但您必须单击“获取”按钮才能显示数据。
#!/usr/bin/python2.7
import Tkinter as tk
import os,sys
import tkFileDialog as tkfd
class E(tk.Frame):
def __init__(self):
tk.Frame.__init__(self)
self.grid()
self.files = {}
self.statusMsg = tk.StringVar()
self.nLines = tk.StringVar()
self.maxLine = tk.StringVar()
self.fileName = tk.StringVar()
tk.Button(self,text='Get a file',command=self.openfile, bd=5).grid(row=1, column=0)
self.fileOption = tk.OptionMenu(self,self.fileName, [], command=self.getIt)
self.fileOption.configure(width=10, bd=5, relief='ridge')
self.fileOption.grid(row=2, column=0)
tk.Label(self,text='Line Count').grid(row=3, column=0)
tk.Label(self,textvariable=self.nLines).grid(row=3, column = 1)
tk.Label(self,text='Max Line').grid(row=4, column=0)
tk.Label(self,textvariable=self.maxLine).grid(row=4, column = 1)
tk.Button(self,text='show file',command=self.getIt, bd=5).grid(row=5)
tk.Label(self,text='Status').grid(row=6, column=0)
tk.Label(self,textvariable=self.statusMsg,width=20).grid(row=6, column = 1)
def openfile(self):
textFile = tkfd.askopenfile(mode='r', defaultextension='.txt',
filetypes=[("text","*.txt"),("All Files","*")],
initialdir='.')
(fileBaseName, fileExt) = os.path.splitext(os.path.basename(textFile.name))
maxLength = -1
lineCount = 0
for line in textFile:
lineCount += 1
maxLength = max(maxLength,len(line))
self.files[fileBaseName] = [maxLength,lineCount]
self.nLines.set(lineCount)
self.maxLine.set(maxLength)
self.statusMsg.set('Opened %s' % fileBaseName)
""" This works to add the file but the command is not
executed when selected in the optionmenu """
self.fileOption['menu'].add_command(label=fileBaseName, command=tk._setit(self.fileName,fileBaseName))
self.fileName.set(fileBaseName)
"""
fileList = sorted(self.files.keys())
for fname in fileList:
self.fileOption['menu'].add_command(label=fname, command=lambda v=fileList: self.getIt)
"""
"""
self.fileOption = tk.OptionMenu(self,self.fileName, fileList, command=self.getIt)
print fileList
"""
"""
self.fileOption['menu'].add_command(label=fileBaseName, command=tk._setit(self.fileName,fileBaseName))
self.fileOption.configure(command=self.getIt)
"""
"""
self.fileOption['menu'].add_command(label=fileBaseName, command=self.getIt)
"""
self.fileName.set(fileBaseName)
def getIt(self):
[maxLength,lineCount] = self.files[self.fileName.get()]
self.nLines.set(lineCount)
self.maxLine.set(maxLength)
self.statusMsg.set('got it')
if __name__ == "__main__":
app = E()
#app.title('Build Roof')
app.mainloop()
cmets 显示了我尝试过的其他东西。
有许多示例将项目添加到选项列表和其他设置命令的示例,但没有一个可以同时执行这两种操作。
附:我想摆脱选项列表中讨厌的第一个空白条目
【问题讨论】:
标签: python tkinter optionmenu