我开始了一个项目,我希望为我公司编写应用程序脚本以捕获迭代信号处理作业的值。 我想包括在需要不同参数来测试的不同模块之间切换的选项。
当我点击左侧的按钮时,我希望数据输入框的内容更改为另一组Labels
和Entry
小部件。我试图将“AAA”的小部件放在另一个类中,但我不知道如何通过按钮单击来初始化类,以便创建/可见类中的小部件。我用.lift()
尝试过,但那很难看。我还想在数据输入框下保留相同的2帧,右边的ScrolledText
,只需使用活动模块。有任何想法吗?有人可以把我推向正确的方向吗?
哦,是的,这是我的第一个项目,除了简单的阅读 - >替换字符串 - >脚本。
以下是代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Tkinter as tk
import json
import tkFileDialog
import sys
import ScrolledText
from Tkinter import*
class testlogger(tk.Tk):
def __init__(self,parent):
tk.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
#--------------------------------------------------------------------------------------
menubar = tk.Menu(self)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu.add_command(label="Open", command=self.load_file)
filemenu.add_command(label="Save", command=self.safe_file)
filemenu.add_command(label="Save as...")
filemenu.add_command(label="Close")
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo")
editmenu.add_separator()
editmenu.add_command(label="Cut")
editmenu.add_command(label="Copy")
editmenu.add_command(label="Paste")
editmenu.add_command(label="Delete")
editmenu.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index")
helpmenu.add_command(label="About...")
menubar.add_cascade(label="Help", menu=helpmenu)
self.config(menu=menubar)
#--------------------------------------------------------------------------------------
treeFrame = tk.Frame(self)
treeFrame.grid(column=0, row=1, sticky='NW')
b1 = Button(treeFrame, text=u'AAA', command=self.AAAlift)
b1.pack(fill='x')
b2 = Button(treeFrame, text=u'RADON', command=self.radonlift)
b2.pack(fill='x')
b3 = Button(treeFrame, text=u'Adaptive Subtract')
b3.pack(fill='x')
b4 = Button(treeFrame, text=u'GMP')
b4.pack(fill='x')
#--------------------------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------------------------
self.enterData = tk.LabelFrame(self, text=" Data Entry ")
self.enterData.grid(column=1, row=1, columnspan=4, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
titleAAA = tk.Label(self.enterData, text="AAA")
titleAAA.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.freqInput = tk.StringVar()
self.freqInput2 = tk.StringVar()
self.thresInput = tk.StringVar()
self.widthInput = tk.StringVar()
self.minFreq = tk.Label(self.enterData, text="Minimum Frequency")
self.minFreq.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minFreqData = tk.Entry(self.enterData, textvariable=self.freqInput)
minFreqData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxFreq = tk.Label(self.enterData, text="Maximum Frequency")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxFreqData = tk.Entry(self.enterData, textvariable=self.freqInput2)
maxFreqData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.threshold = tk.Label(self.enterData, text="Threshold")
self.threshold.grid(row=2, column=3, sticky='E', padx=5, pady=2)
thresData = tk.Entry(self.enterData, textvariable=self.thresInput)
thresData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.width = tk.Label(self.enterData, text="SpatialWidth")
self.width.grid(row=3, column=3, sticky='E', padx=5, pady=2)
widthData = tk.Entry(self.enterData, textvariable=self.widthInput)
widthData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.freqInput, self.freqInput2, self.thresInput, self.widthInput]
self.labelList = [self.minFreq, self.maxFreq, self.threshold, self.width]
enteredAAAData = []
#--------------------------------------------------------------------------------------
self.radon = LabelFrame(self, text=" Data Entry ")
self.radon.grid(column=1, row=1, columnspan=4, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
titleRadon = Label(self.radon, text="Radon Fwd Transform")
titleRadon.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.minMOInput = StringVar()
self.maxMOInput = StringVar()
self.offsetInput = StringVar()
self.numpInput = StringVar()
self.motypeInput = StringVar()
self.maxfreqInput = StringVar()
self.minMO = Label(self.radon, text="Minimum Moveout")
self.minMO.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minMOData = Entry(self.radon, textvariable=self.minMOInput)
minMOData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxMO = Label(self.radon, text="Maximum Moveout")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxMOData = Entry(self.radon, textvariable=self.maxMOInput)
maxMOData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.offset = Label(self.radon, text="Reference Offset")
self.offset.grid(row=2, column=3, sticky='E', padx=5, pady=2)
offsetData = Entry(self.radon, textvariable=self.offsetInput)
offsetData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.numP = Label(self.radon, text="Number of P-Traces")
self.numP.grid(row=3, column=3, sticky='E', padx=5, pady=2)
numPData = Entry(self.radon, textvariable=self.numpInput)
numPData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.motype = Label(self.radon, text="Moveout Type")
self.motype.grid(row=4, column=1, sticky='E', padx=5, pady=2)
motypeData = Entry(self.radon, textvariable=self.motypeInput)
motypeData.grid(row=4, column=2, sticky='E', padx=5, pady=2)
self.maxfreq = Label(self.radon, text="Maximum Frequency")
self.maxfreq.grid(row=4, column=3, sticky='E', padx=5, pady=2)
maxfreqData = Entry(self.radon, textvariable=self.maxfreqInput)
maxfreqData.grid(row=4, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.minMOInput, self.maxMOInput, self.offsetInput, self.numpInput, self.motypeInput, self.maxfreqInput]
self.labelList = [self.minMO, self.maxMO, self.offset, self.numP, self.motype, self.maxfreq]
enteredRadonData = []
#--------------------------------------------------------------------------------------
evalData = tk.Frame(self)
evalData.grid(column=1, row=2, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
evalLabel = tk.Label(evalData, text="Evaluation")
evalLabel.grid(row=1, column=1, columnspan=3, sticky='N', padx=5, pady=2)
self.eval_state = tk.StringVar()
goodRadio = tk.Radiobutton(evalData, text="Good", variable=self.eval_state, value='Good')
goodRadio.grid(row=2, column=1, sticky='W', padx=5, pady=2)
badRadio = tk.Radiobutton(evalData, text="Bad", variable=self.eval_state, value='Bad')
badRadio.grid(row=2, column=2, sticky='W', padx=5, pady=2)
sameRadio = tk.Radiobutton(evalData, text="Same", variable=self.eval_state, value='Same')
sameRadio.grid(row=2, column=3, sticky='W', padx=5, pady=2)
qcDisp = tk.Label(evalData, text="QC Displays:")
qcDisp.grid(row=2,column=4, sticky='W', padx=10, pady=2)
self.checkB4 = tk.IntVar()
optionBefore = tk.Checkbutton(evalData, text="Before", variable=self.checkB4, onvalue=1, offvalue=0)
optionBefore.grid(row=1, column=5, sticky='W', padx=5, pady=2)
optionAfter = tk.Checkbutton(evalData, text="After", onvalue=1, offvalue=0)
optionAfter.grid(row=2, column=5, sticky='W', padx=5, pady=2)
optionDiff = tk.Checkbutton(evalData, text="Difference", onvalue=1, offvalue=0)
optionDiff.grid(row=3, column=5, sticky='W', padx=5, pady=2)
optionSpectrum = tk.Checkbutton(evalData, text="Frequency Spectrum", onvalue=1, offvalue=0)
optionSpectrum.grid(row=1, column=6, sticky='W', padx=5, pady=2)
optionAuto = tk.Checkbutton(evalData, text="Auto Correlation", onvalue=1, offvalue=0)
optionAuto.grid(row=2, column=6, sticky='W', padx=5, pady=2)
optionBanana = tk.Checkbutton(evalData, text="I'm a Banana", onvalue=1, offvalue=0)
optionBanana.grid(row=3, column=6, sticky='W', padx=5, pady=2)
#--------------------------------------------------------------------------------------
self.comment = tk.Text(self, height=5, bg='white')
self.comment.grid(column=1, row=3, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
submit_b = tk.Button(self, text=u"Submit", command=self.enter_data)
submit_b.grid(column=1, row=4, sticky='NE')
#--------------------------------------------------------------------------------------
protocolFrame = tk.LabelFrame(self, text="Protocol")
protocolFrame.grid(row=1, column=2, rowspan=3, sticky='NW')
self.protocolText = ScrolledText.ScrolledText(protocolFrame,state='disabled',height=30, width=70)
self.protocolText.grid(column=0, row=0, sticky='NE')
#--------------------------------------------------------------------------------------
self.L = []
#--------------------------------------------------------------------------------------
def enter_data(self):
x = self.labelList[0].cget('text')
y = self.valueList[0].get()
self.L.append(x + ': ' + y)
x = self.labelList[1].cget('text')
y = self.valueList[1].get()
self.L.append(x + ': ' + y)
x = self.labelList[2].cget('text')
y = self.valueList[2].get()
self.L.append(x + ': ' + y)
x = self.labelList[3].cget('text')
y = self.valueList[3].get()
self.L.append(x + ': ' + y)
x = self.eval_state.get()
self.L.append(x)
if self.checkB4 == 1:
y = self.optionBefore.cget('text')
self.L.append(y)
x = self.comment.get('1.0','end')
self.L.append(x)
self.L.append('-------------------------------------\n')
self.protocolText.config(state='normal')
self.protocolText.insert(tk.END, '\n'.join(self.L))
self.protocolText.config(state='disabled')
self.L = []
#------------------------------------------------------------------------------------------
def safe_file(self):
s = self.protocolText.get('1.0','end')
with open('my_json', 'w') as fp:
json.dump(s, fp)
def load_file(self):
options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialdir'] = '/home'
options['parent'] = self.parent
options['title'] = "Open a file"
self.protocolText.config(state='normal')
with tkFileDialog.askopenfile(mode='r', **options) as f_handle:
for line in f_handle:
self.protocolText.insert(tk.END, f_handle)
self.protocolText.config(state='disabled')
def AAAlift(self):
self.enterData.lift()
def radonlift(self):
self.radon.lift()
if __name__ == "__main__":
app = testlogger(None)
app.title('Testlogger')
app.mainloop()
应用开始时:
按下AAA时:
答案 0 :(得分:0)
我稍微修改了你的代码。基本上我添加了containerFrame,以包装LabelFrames。我认为通过这种方式更容易使它更容易。我还在其他地方添加了一些填充,以使协议部分与其他部分内联。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Tkinter as tk
import json
import tkFileDialog
import sys
import ScrolledText
from Tkinter import*
class testlogger(tk.Tk):
def __init__(self,parent):
tk.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
#--------------------------------------------------------------------------------------
menubar = tk.Menu(self)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu.add_command(label="Open", command=self.load_file)
filemenu.add_command(label="Save", command=self.safe_file)
filemenu.add_command(label="Save as...")
filemenu.add_command(label="Close")
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo")
editmenu.add_separator()
editmenu.add_command(label="Cut")
editmenu.add_command(label="Copy")
editmenu.add_command(label="Paste")
editmenu.add_command(label="Delete")
editmenu.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index")
helpmenu.add_command(label="About...")
menubar.add_cascade(label="Help", menu=helpmenu)
self.config(menu=menubar)
#--------------------------------------------------------------------------------------
treeFrame = tk.Frame(self)
treeFrame.grid(column=0, row=1, sticky='NW')
b1 = Button(treeFrame, text=u'AAA', command=self.AAAlift)
b1.pack(fill='x')
b2 = Button(treeFrame, text=u'RADON', command=self.radonlift)
b2.pack(fill='x')
b3 = Button(treeFrame, text=u'Adaptive Subtract')
b3.pack(fill='x')
b4 = Button(treeFrame, text=u'GMP')
b4.pack(fill='x')
#--------------------------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------------------------
self.containerFrame = tk.Frame(self)
self.containerFrame.grid(column=1, row=1, columnspan=4, sticky='NW', \
padx=5, pady=5, ipadx=1, ipady=1)
self.enterData = tk.LabelFrame(self.containerFrame, text=" Data Entry ")
#self.enterData.pack(fill=tk.BOTH, expand=1)
self.enterData.grid(column=0, row=0, sticky='NW', ipadx=17)
# self.enterData.grid(column=1, row=1, columnspan=4, sticky='NW', \
# padx=5, pady=5, ipadx=5, ipady=5)
titleAAA = tk.Label(self.enterData, text="AAA")
titleAAA.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.freqInput = tk.StringVar()
self.freqInput2 = tk.StringVar()
self.thresInput = tk.StringVar()
self.widthInput = tk.StringVar()
self.minFreq = tk.Label(self.enterData, text="Minimum Frequency")
self.minFreq.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minFreqData = tk.Entry(self.enterData, textvariable=self.freqInput)
minFreqData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxFreq = tk.Label(self.enterData, text="Maximum Frequency")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxFreqData = tk.Entry(self.enterData, textvariable=self.freqInput2)
maxFreqData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.threshold = tk.Label(self.enterData, text="Threshold")
self.threshold.grid(row=2, column=3, sticky='E', padx=5, pady=2)
thresData = tk.Entry(self.enterData, textvariable=self.thresInput)
thresData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.width = tk.Label(self.enterData, text="SpatialWidth")
self.width.grid(row=3, column=3, sticky='E', padx=5, pady=2)
widthData = tk.Entry(self.enterData, textvariable=self.widthInput)
widthData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.freqInput, self.freqInput2, self.thresInput, self.widthInput]
self.labelList = [self.minFreq, self.maxFreq, self.threshold, self.width]
enteredAAAData = []
#--------------------------------------------------------------------------------------
self.radon = LabelFrame(self.containerFrame, text=" Data Entry ")
#self.radon.pack(fill=tk.BOTH, expand=1)
self.radon.grid(column=0, row=0, sticky='NW', ipadx=0)
titleRadon = Label(self.radon, text="Radon Fwd Transform")
titleRadon.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.minMOInput = StringVar()
self.maxMOInput = StringVar()
self.offsetInput = StringVar()
self.numpInput = StringVar()
self.motypeInput = StringVar()
self.maxfreqInput = StringVar()
self.minMO = Label(self.radon, text="Minimum Moveout")
self.minMO.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minMOData = Entry(self.radon, textvariable=self.minMOInput)
minMOData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxMO = Label(self.radon, text="Maximum Moveout")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxMOData = Entry(self.radon, textvariable=self.maxMOInput)
maxMOData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.offset = Label(self.radon, text="Reference Offset")
self.offset.grid(row=2, column=3, sticky='E', padx=5, pady=2)
offsetData = Entry(self.radon, textvariable=self.offsetInput)
offsetData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.numP = Label(self.radon, text="Number of P-Traces")
self.numP.grid(row=3, column=3, sticky='E', padx=5, pady=2)
numPData = Entry(self.radon, textvariable=self.numpInput)
numPData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.motype = Label(self.radon, text="Moveout Type")
self.motype.grid(row=4, column=1, sticky='E', padx=5, pady=2)
motypeData = Entry(self.radon, textvariable=self.motypeInput)
motypeData.grid(row=4, column=2, sticky='E', padx=5, pady=2)
self.maxfreq = Label(self.radon, text="Maximum Frequency")
self.maxfreq.grid(row=4, column=3, sticky='E', padx=5, pady=2)
maxfreqData = Entry(self.radon, textvariable=self.maxfreqInput)
maxfreqData.grid(row=4, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.minMOInput, self.maxMOInput, self.offsetInput, self.numpInput, self.motypeInput, self.maxfreqInput]
self.labelList = [self.minMO, self.maxMO, self.offset, self.numP, self.motype, self.maxfreq]
enteredRadonData = []
#--------------------------------------------------------------------------------------
evalData = tk.Frame(self)
evalData.grid(column=1, row=2, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
evalLabel = tk.Label(evalData, text="Evaluation")
evalLabel.grid(row=1, column=1, columnspan=3, sticky='N', padx=5, pady=2)
self.eval_state = tk.StringVar()
goodRadio = tk.Radiobutton(evalData, text="Good", variable=self.eval_state, value='Good')
goodRadio.grid(row=2, column=1, sticky='W', padx=5, pady=2)
badRadio = tk.Radiobutton(evalData, text="Bad", variable=self.eval_state, value='Bad')
badRadio.grid(row=2, column=2, sticky='W', padx=5, pady=2)
sameRadio = tk.Radiobutton(evalData, text="Same", variable=self.eval_state, value='Same')
sameRadio.grid(row=2, column=3, sticky='W', padx=5, pady=2)
qcDisp = tk.Label(evalData, text="QC Displays:")
qcDisp.grid(row=2,column=4, sticky='W', padx=10, pady=2)
self.checkB4 = tk.IntVar()
optionBefore = tk.Checkbutton(evalData, text="Before", variable=self.checkB4, onvalue=1, offvalue=0)
optionBefore.grid(row=1, column=5, sticky='W', padx=5, pady=2)
optionAfter = tk.Checkbutton(evalData, text="After", onvalue=1, offvalue=0)
optionAfter.grid(row=2, column=5, sticky='W', padx=5, pady=2)
optionDiff = tk.Checkbutton(evalData, text="Difference", onvalue=1, offvalue=0)
optionDiff.grid(row=3, column=5, sticky='W', padx=5, pady=2)
optionSpectrum = tk.Checkbutton(evalData, text="Frequency Spectrum", onvalue=1, offvalue=0)
optionSpectrum.grid(row=1, column=6, sticky='W', padx=5, pady=2)
optionAuto = tk.Checkbutton(evalData, text="Auto Correlation", onvalue=1, offvalue=0)
optionAuto.grid(row=2, column=6, sticky='W', padx=5, pady=2)
optionBanana = tk.Checkbutton(evalData, text="I'm a Banana", onvalue=1, offvalue=0)
optionBanana.grid(row=3, column=6, sticky='W', padx=5, pady=2)
#--------------------------------------------------------------------------------------
self.comment = tk.Text(self, height=5, bg='white')
self.comment.grid(column=1, row=3, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
submit_b = tk.Button(self, text=u"Submit", command=self.enter_data)
submit_b.grid(column=1, row=4, sticky='NE')
#--------------------------------------------------------------------------------------
protocolFrame = tk.LabelFrame(self, text="Protocol")
protocolFrame.grid(row=1, column=2, padx=45, pady=5, rowspan=3, sticky='NW')
self.protocolText = ScrolledText.ScrolledText(protocolFrame,state='disabled',height=30, width=70)
self.protocolText.grid(column=0, row=0, sticky='NE')
#--------------------------------------------------------------------------------------
self.L = []
#--------------------------------------------------------------------------------------
def enter_data(self):
x = self.labelList[0].cget('text')
y = self.valueList[0].get()
self.L.append(x + ': ' + y)
x = self.labelList[1].cget('text')
y = self.valueList[1].get()
self.L.append(x + ': ' + y)
x = self.labelList[2].cget('text')
y = self.valueList[2].get()
self.L.append(x + ': ' + y)
x = self.labelList[3].cget('text')
y = self.valueList[3].get()
self.L.append(x + ': ' + y)
x = self.eval_state.get()
self.L.append(x)
if self.checkB4 == 1:
y = self.optionBefore.cget('text')
self.L.append(y)
x = self.comment.get('1.0','end')
self.L.append(x)
self.L.append('-------------------------------------\n')
self.protocolText.config(state='normal')
self.protocolText.insert(tk.END, '\n'.join(self.L))
self.protocolText.config(state='disabled')
self.L = []
#------------------------------------------------------------------------------------------
def safe_file(self):
s = self.protocolText.get('1.0','end')
with open('my_json', 'w') as fp:
json.dump(s, fp)
def load_file(self):
options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialdir'] = '/home'
options['parent'] = self.parent
options['title'] = "Open a file"
self.protocolText.config(state='normal')
with tkFileDialog.askopenfile(mode='r', **options) as f_handle:
for line in f_handle:
self.protocolText.insert(tk.END, f_handle)
self.protocolText.config(state='disabled')
def AAAlift(self):
#self.enterData.grid_forget()
self.radon.grid_remove()
self.enterData.grid()
def radonlift(self):
self.enterData.grid_remove()
self.radon.grid()
if __name__ == "__main__":
app = testlogger(None)
app.title('Testlogger')
app.mainloop()
截图是:
显然,需要做更多的工作才能使所有内容“均匀”并相互内联,但按下AAA和RADON按钮可以按预期工作(我认为)。您可以扩展/修改我的修改,希望它可以让您达到预期的效果。