I am trying to use a ttk.notebook in python3 on a Linux (Ubuntu 14.04LTS) system to read Blockquote`# -- coding: utf-8 --data from experiments and perform several curve fits on this data in order to calculate the coefficients needed for a Finite Element calculation. The actual calculation of these curves I have already performed without tkinter. I am now setting up the user interface. All works well, until I want to fill in the variable a as global variable, noting is exported to the Ipython shell (using spyder2). The variables: CreepData and path2 which are made global in the same way are visble in the Ipython shell. I am not sure if the way I am extracting values from the combo boxes or the Entry fields on the notebook page select units and run work in the intended way. See the code below:
""" Created on Sat Jan 16 18:56:16 2016 @author: peterk Derived frpm a lot of posted examples """ import csv from copy import deepcopy import numpy as np import matplotlib import scipy import matplotlib matplotlib.use("TkAgg") from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg from matplotlib.figure import Figure from matplotlib import pyplot as plt from scipy import stats from scipy.optimize import curve_fit from tkinter import * import tkinter as tk import tkinter.font as tkFont import tkinter.ttk as ttk from 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 the notebook nb = ttk.Notebook(mainPanel, name='notebook') # extend bindings to 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 the app frame = ttk.Frame(nb, name='readMeFirst') # widgets to be displayed on 'Description' tab msg = ["Ttk is the new Tk themed widget set. One of the widgets ", "it includes is the notebook widget, which provides a set ", "of tabs that allow the selection of a group of panels, ", "each with distinct content. They are a feature of many ", "modern user interfaces. Not only can the tabs be selected ", "with the mouse, but they can also be switched between ", "using Ctrl+Tab when the notebook page heading itself is ", "selected. Note that the second tab is disabled, and cannot " "be selected." " aaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "ccccccccccccccccccccccccccccccccccccccccccccccccccccccc", "dddddd", "eeeee", "f", "a", "b", "c", "d", "e", "f", "g", "h", " here is 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 and 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 for button short-cut key # (must be bound to topplevel window) self.winfo_toplevel().bind('<Alt-n>', lambda e, v=neatVar: self._say_neat(v)) # add to notebook (underline = index for short-cut character) nb.add(frame, text='ReadMeFirst', underline=0, padding=2) def _say_neat(self, v): v.set('Yeah, I know...') self.update() self.after(500, v.set('')) # return path2 # ============================================================================= def _create_import_data_tab(self, nb): # Populate the 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 the third frame with a 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, I know...') # self.update() # self.after(500, v.set('')) # add to notebook (underline = index for short-cut character) nb.add(frame, text='Text Editor', underline=0) #============================================================ def _create_select_units_run_tab(self, nb): # select units and perform the calculation frame = ttk.Frame(nb, name="select units and calculate") global l global k global sigma global creepData global a 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 the required fields and push the run button') cbl1 = ttk.Label(frame, text='Select units used in your 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 the 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 the 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 and 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 is copied') self.update() self.after(500, w.set('')) #=================================================================== if __name__ == '__main__': CreepNotebook().mainloop()`
If needed I can upload the csv.files on my repro, but I do not think it is needed for answering the question. The run1 function would be used to fit the data curves and return a message that the calculations were performed.