i trying use ttk.notebook in python3 on linux (ubuntu 14.04lts) system read blockquote`# -- coding: utf-8 --data experiments , perform several curve fits on data in order calculate coefficients needed finite element calculation. actual calculation of these curves have performed without tkinter. setting user interface. works well, until want fill in variable global variable, noting exported ipython shell (using spyder2). variables: creepdata , path2 made global in same way visble in ipython shell. not sure if way extracting values combo boxes or entry fields on notebook page select units , run work in intended way. see code below:
""" created on sat jan 16 18:56:16 2016 @author: peterk derived frpm lot of posted examples """ import csv copy import deepcopy import numpy np import matplotlib import scipy import matplotlib matplotlib.use("tkagg") matplotlib.backends.backend_tkagg import figurecanvastkagg,navigationtoolbar2tkagg matplotlib.figure import figure matplotlib import pyplot plt scipy import stats scipy.optimize import curve_fit tkinter import * import tkinter tk import tkinter.font tkfont import tkinter.ttk ttk tkinter import filedialog path2="./creep.csv" class creepnotebook(ttk.frame): def __init__(self, isapp=true, name='notebookdemo'): ttk.frame.__init__(self, name=name) self.pack(expand=true, fill="both") self.master.title('creep fit') self.isapp = isapp self._create_widgets() self.master.minsize(1920,1000) def _create_widgets(self): self._create_main_panel() def _create_main_panel(self): mainpanel = ttk.frame(self, name='demo') mainpanel.pack( expand=true, side="top", fill="both") # create notebook nb = ttk.notebook(mainpanel, name='notebook') # extend bindings top level window allowing # ctrl+tab - cycles thru tabs # shift+ctrl+tab - previous tab # alt+k - select tab using mnemonic (k = underlined letter) nb.enable_traversal() nb.pack(fill="both", padx=2, pady=3,expand=true) self._create_readme_tab(nb) self._create_import_data_tab(nb) self._create_select_units_run_tab(nb) self._create_text_tab(nb) def _create_readme_tab(self, nb): # frame explaining app frame = ttk.frame(nb, name='readmefirst') # widgets displayed on 'description' tab msg = ["ttk new tk themed widget set. 1 of widgets ", "it includes notebook widget, provides set ", "of tabs allow selection of group of panels, ", "each distinct content. feature of many ", "modern user interfaces. not can tabs selected ", "with mouse, can switched between ", "using ctrl+tab when notebook page heading ", "selected. note second tab disabled, , cannot " "be selected." " aaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "ccccccccccccccccccccccccccccccccccccccccccccccccccccccc", "dddddd", "eeeee", "f", "a", "b", "c", "d", "e", "f", "g", "h", " here text too"] lbl = ttk.label(frame, wraplength='4i', justify=tk.left, anchor=tk.center, text=''.join(msg)) neatvar = tk.stringvar() btn = ttk.button(frame, text='neat!', underline=0, command=lambda v=neatvar: self._say_neat(v)) neat = ttk.label(frame, textvariable=neatvar, name='neat') # position , set resize behaviour lbl.grid(row=0, column=0, columnspan=2, sticky='new', pady=5) btn.grid(row=1, column=0, pady=(2,4)) neat.grid(row=1, column=1, pady=(2,4)) frame.rowconfigure(1, weight=1) frame.columnconfigure((0,1), weight=1, uniform=1) # bind button short-cut key # (must bound topplevel window) self.winfo_toplevel().bind('<alt-n>', lambda e, v=neatvar: self._say_neat(v)) # add notebook (underline = index short-cut character) nb.add(frame, text='readmefirst', underline=0, padding=2) def _say_neat(self, v): v.set('yeah, know...') self.update() self.after(500, v.set('')) # return path2 # ============================================================================= def _create_import_data_tab(self, nb): # populate second pane. frame = ttk.frame(nb, name="import data") global l global k global sigma global creepdata global filen global path2 butn=ttk.button(frame, text='select csv file', command=self.askopenfilename2) butn.pack(side="top") self.file_opt = options = {} options['defaultextension'] = '.csv' options['filetypes'] =[('csv files', '.csv'),('all files', '.*')] options['initialdir'] = '.' options['initialfile'] = 'creep2.csv' options['parent'] = nb options['title'] = 'select csv file' global creepdata print("path2 in _create_import_data_tab") print (path2) nb.add(frame, text='import data', underline=0) def askopenfilename2(self): global path2 global creepdata path2 = filedialog.askopenfilename(**self.file_opt) print("path2 in askopenfilename2") print(path2) creepreader=csv.reader(open(path2, newline=""), delimiter=',') creepdata=list(creepreader) #enter code here ======================================== def _create_text_tab(self, nb): # populate third frame text widget frame = ttk.frame(nb) txt = tk.text(frame, width=40, height=10) vscroll = ttk.scrollbar(frame, orient=tk.vertical, command=txt.yview) txt['yscroll'] = vscroll.set vscroll.pack(side=tk.right) # txt.pack(tk.fill=both, tk.expand=true) txt.pack() # btn2l.pack(side="top", pady=5) # btn2.pack(side="top", pady=2) # w.pack(side="top", pady=2) # neatvar = tk.stringvar() # btn = ttk.button(frame, text='neat!', underline=0, # command=lambda v=neatvar: self._say_neat(v)) # neat = ttk.label(frame, textvariable=neatvar, name='neat') # def _say_neat(self, v): # v.set('yeah, know...') # self.update() # self.after(500, v.set('')) # add notebook (underline = index short-cut character) nb.add(frame, text='text editor', underline=0) #============================================================ def _create_select_units_run_tab(self, nb): # select units , perform calculation frame = ttk.frame(nb, name="select units , calculate") global l global k global sigma global creepdata global a=tk.stringvar() frame.grid(column=0, row=0, rowspan=12, columnspan=5,sticky=(n,s,e,w)) units = ('m,kg,s', 'mm,mg,s', 'mm,kg,ms') a=tk.stringvar() cbl0 = ttk.label(frame, text='select or fill in required fields , push run button') cbl1 = ttk.label(frame, text='select units used in fe-prgram') cb1 = ttk.combobox(frame, value=units, state='readonly') # cbl1.pack(side="top") # cb1.pack(side="top") time_units=('hrs', 's','ms') cbl2=ttk.label(frame, text='select time units used in csv file') cb2 = ttk.combobox(frame, value=time_units, state='readonly') # cbl2.pack(side="top") # cb2.pack(side="top") strain_units=('strain [%]', 'strain [-]','creepmodulus [mpa]') cbl3=ttk.label(frame, text='select strain or modulus units used in csv file') cb3 = ttk.combobox(frame, value=strain_units, state='readonly') # cbl3.pack(side="top") # cb3.pack(side="top") yml=ttk.label(frame, text=' input anisotropic youngs modulus in mpa') ym=entry(frame) # yml.pack(side="top") # ym.pack(side="top") isfl=ttk.label(frame, text='input isotropic factor') isf=entry(frame) # isfl.pack(side="top") # isf.pack(side="top") run1var = tk.stringvar() btn2 = ttk.button(frame, text='run', underline=0, command=lambda w=run1var: self._run1(w)) btn2l = ttk.label(frame, textvariable=run1var, name='run1') cbl0.grid(column=0, row=0, sticky=w, pady=100) cbl1.grid(column=6, row=1, sticky=w, pady=25) cb1.grid(column=6, row=2, sticky=w, pady=2) cbl2.grid(column=6, row=3, sticky=w, pady=25) cb2.grid(column=6, row=4, sticky=w, pady=2) cbl3.grid(column=6, row=5, sticky=w, pady=25) cb3.grid(column=6, row=6, sticky=w, pady=2) yml.grid(column=6, row=7, sticky=w, pady=25) ym.grid(column=6, row=8, sticky=w ,pady=2) isfl.grid(column=6, row=9, sticky=w, pady=25) isf.grid(column=6, row=10, sticky=w, pady=2) btn2l.grid(column=6, row=11, sticky=w, pady=25) btn2.grid(column=6, row=12, sticky=w, pady=2) nb.add(frame, text='select data , run', underline=0, padding=2) a=cb1.get() print(a) print(cb1.get()) yms=ym.get() isfs=isf.get() def _run1(self, w): # global creepdata # global creepdat # creepdat=deepcopy(creepdata) w.set('creepdata copied') self.update() self.after(500, w.set('')) #=================================================================== if __name__ == '__main__': creepnotebook().mainloop()`
if needed can upload csv.files on repro, not think needed answering question. run1 function used fit data curves , return message calculations performed.
changed - sorry did not understand question, prehaps more helpful:
first i'd recommend taking @ both of these:
share variables between methods
second, creating input entries in _create_select_units_run_tab
, do:
def _create_select_units_run_tab: ... = cb1.get() ...
this contents after being created , before .mainloop()
called absolutely empty string, in order content later in _run1
need keep track of entries instead:
def _create_select_units_run_tab: ... self.input_fields = [cb1,cb2,cb3,ym,isf] #this give other methods access input fields #then them can use this: def get_inputs(self): return [entry.get() entry in self.input_fields] #then in run can data in fields def _run1(self, w): inp_data = self.get_inputs() print(inp_data) if "" in inp_data: w.set("you need fill everthing in first!") else: w.set(str(inp_data)) ...
while testing code cancelled file dialog window threw error because path2 == ""
may want check:
def askopenfilename2(self): ... if path2 not "": creepreader=csv.reader(open(path2, newline=""), delimiter=',') creepdata = list(creepreader) #else: # creepdata = none # or leave last used value? ...
not mention open()
never closed (input , output python documentation):
it practice use keyword when dealing file objects. has advantage file closed after suite finishes, if exception raised on way. shorter writing equivalent `try-finally` blocks:open('workfile', 'r') f: ... read_data = f.read() >>> f.closed true
so may want implement too:
open(path2, newline="") file_obj: creepreader=csv.reader(file_obj, delimiter=',')
lastly i'd draw attention to:
self.after(500,w.set('')
w.set('')
being called , using return value none
in self.after
, instead call w.set
argument need this:
self.after(500,w.set,'')
then tkinter call after 500ms forwarding arguments receives in after
there may other issues code missed hope helped.
Comments
Post a Comment