python - calling plt.figure prevents Checkbutton from update - Tkinter - python-3.x

A GUI that works with different pages, one is a graph page using the animate function, and a second page should start a guide, the guide will depend on what you click in a checkbutton. I have created a post because I thought I was the checkbutton that was not well defined, but now i found that calling the f=plt.figure() is prevent the upgrade of the checkbutton.
See this code works for the Checkbutton: - Before the animate function .. But then the graphic update on the other page does not work since f is not defined in the animate function. If i put the animate function and f definition in the PageThree (GRAPHIC page) the update of the figure fails.
But when i allow the f=plt.figure() , graphic is fine, but checkbutton stays False all the time...
import matplotlib
from tkinter import *
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import tkinter as tk
from tkinter import ttk
import numpy as np
import matplotlib.animation as animation
from matplotlib import style
from matplotlib import pyplot as plt
from scipy.misc import *
import pandas as pd
import webbrowser
import subprocess
import csv
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from os import startfile
from time import time
LARGE_FONT = ("Times", 11, "bold italic")
NORM_FONT = ("Helvetica", 9)
SMALL_FONT = ("Helvetica",7)
HELP_FONT=("Times", 9 , "bold")
pullDataMS=pd.read_table("test.raw.spectrum_table.tsv", header=1)
LevelMS=pullDataMS.groupby('msLevel')
rt=pullDataMS['rt']
TIC=pullDataMS['TIC']
MS1=LevelMS.get_group('ms1')
MS2=LevelMS.get_group('ms2')
### THIS CALL overplots the update? -.- ###
#f = plt.figure(facecolor="white")**
######################
def animate (i):
pullData=pd.read_table("InstrumentPerformance.txt", parse_dates=True,
dayfirst=True, index_col=1)
Names_In = ['Q1','Q2','Q3']
Inst=pullData.groupby('Instrument')
a = f.add_subplot(2,3,1)
a2 = f.add_subplot(2,3,2, sharex=a)
a3=f.add_subplot(2,3,3)
a4=f.add_subplot(2,3,4)
a5=f.add_subplot(2,3,5)
a7=f.add_subplot(2,6,11)
f.subplots_adjust(wspace=0.29)
a.clear()
a2.clear()
a3.clear()
a4.clear()
a5.clear()
a7.clear()
for Name in Names_In:
Q=Inst.get_group(Name)
y=Q['MS/MS recorded']
x=Q['Identified peptides']
a.plot(x, "o",label='['+Name+"]")
a.plot(x, 'k-', lw=0.7, label='_nolegend_')
a2.plot(y, 'o',label='['+Name+']')
a2.plot(y, 'k-', label='_nolegend_', lw=0.7)
a.xaxis.set_major_locator(mticker.MaxNLocator(5))
a.xaxis.set_major_formatter(mdates.DateFormatter("%d-%m-%Y"))
a.set_ylabel('# of unique peptide sequences')
a2.set_ylabel('# of MS/MS spectra')
a.legend(loc='upper left',fancybox=True, numpoints=1, shadow=True,prop={'size':9})
a2.legend(loc='upper left',fancybox=True, numpoints=1, shadow=True,prop={'size':9})
a.set_xlim(np.array([-20,20])+a.get_xlim())
a.set_ylim(np.array([0,2000])+a.get_ylim())
a2.set_xlim(np.array([-20,20])+a.get_xlim())
a2.set_ylim(np.array([0,5000])+a.get_ylim())
class PAL3_guide(tk.Tk):
def __init__(self,*args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,Tut01,PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame=self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self,parent, controller):
tk.Frame.__init__(self,parent, background="white")
button_PAL = ttk.Button(self, text="Setup Guide", command=lambda:controller.show_frame(Tut01))
button_PAL.pack()
button2 = ttk.Button(self, text="Graph Page 1",
command=lambda: controller.show_frame(PageThree))
button2.pack()
class Tut01(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.parent=parent
self.initUI()
def initUI(self):
self.var=BooleanVar()
cb=Checkbutton(self,text="show", variable=self.var, command=self.onClick)
cb.select()
cb.place(x=50,y=50)
def onClick(self):
if self.var.get()== True:
print("hi")
else:
print("buh")
class PageThree(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="GraphPage", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
canvas = FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
toolbar=NavigationToolbar2TkAgg(canvas,self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP,fill=tk.BOTH, expand=True)
app = PAL3_guide()
app.geometry("1280x920")
ani = animation.FuncAnimation(f, animate, interval=10000)
app.mainloop()
EDIT FOR MIXONE: yes i have tried to do something like :
def animate (f):
pullData=pd.read_table("InstrumentPerformance.txt", parse_dates=True,
dayfirst=True, index_col=1)
Names_In = ['Q1','Q2','Q3']
Inst=pullData.groupby('Instrument')
a = f.add_subplot(2,3,1)
a2 = f.add_subplot(2,3,2, sharex=a)...
...
and:
class PageThree(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="GraphPage", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
f=plt.figure()
ani = animation.FuncAnimation(f, animate(f), interval=10000)
canvas = FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
#toolbar=NavigationToolbar2TkAgg(canvas,self)
#toolbar.update()
#canvas._tkcanvas.pack(side=tk.TOP,fill=tk.BOTH, expand=True)
app = PAL3_guide()
app.geometry("1280x920")
app.mainloop()
In this case the Checkbutton works, but the figure is not updated but it is plotted in the beginning.

Related

How to Write Data to a File and Plot that File in TK LIVE

I have a sensor that is measuring temperature every second and i'm writing that data to a text file (.txt). I want to have a plot that updates AS the data is written to the file - a live plotter if you will. I have the majority of the framework written to plot the data and have a plot in the GUI that i'm still working on. That code is attached. As it is now, I can execute the code and open up the plotter. As I type data into the file manually and save it, the plot updates. But like I said, i'm looking to have it do that without me opening the file and manually saving it. The sensor would write to the file each second and the plot would just automatically update. In lieu of the sensor code, i've put in a rough skeleton set of code where the sensor code will go.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
from time import sleep
import tkinter as tk
from tkinter import ttk
LARGE_FONT= ("Verdana", 12)
style.use("ggplot")
f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)
# =============================================================================
# SENSOR DATA INPUT GOES HERE.
# x = random.random()
# y = random.random()
# f = "test_data.txt"
# f.open()
# f.write("test_data.txt,"a") #write to the file in x y format, no comma in between as the
deliniator is a space.
# =============================================================================
def animate(i):
pullData = open("test_data.txt","r").read()
dataList = pullData.split('\n')
xList = []
yList = []
for eachLine in dataList:
if len(eachLine) > 1:
x, y = eachLine.split(' ')
xList.append(float(x)) #int(x)
yList.append(float(y)) #int(y)
a.clear()
a.plot(xList, yList)
class Data_Acq(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# tk.Tk.iconbitmap(self, default="clienticon.ico")
tk.Tk.wm_title(self, "Data Acquisition")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Experiment Alpha", font=LARGE_FONT)
label.pack(pady=10,padx=10)
#button = ttk.Button(self, text="Visit Page 1",
# command=lambda: controller.show_frame(PageOne))
#button.pack()
#button2 = ttk.Button(self, text="Visit Page 2",
# command=lambda: controller.show_frame(PageTwo))
#button2.pack()
button3 = ttk.Button(self, text="Graph Page",
command=lambda: controller.show_frame(PageThree))
button3.pack()
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Graph Page!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
canvas = FigureCanvasTkAgg(f, self)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
app = Data_Acq()
ani = animation.FuncAnimation(f, animate, interval=500)
app.mainloop()
So again, as I enter data into the text file in this format:
1 22.0
2 33.1
3 33.0
4 33.5
5 41.2
and hit save on each entry, it updates the graph.

Getting 2 separate tkinter windows instead of navigating through 3 pages

I am trying to combine my graphs from matplot into the tkinter window and be able to navigate through the different graphs. I have tried experimenting for now and have one graph on it. However, I get 2 windows from tkinter instead of 1.
I've inserted the code I have done so far:
import tkinter as tk
from tkinter import ttk
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2Tk
from tkinter import *
import tkinter.messagebox as tm
LARGE_FONT=("Verdana", 12) #font type and font size
df1= pd.read_csv(r"U:\\GE90\nodes_fixed.csv")
df2 = pd.read_csv(r"U:\\GE90\edge_list_3_fixed.csv")
g=nx.Graph()
# Add edges and edge attributes
for i, elrow in df2.iterrows():
# g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict()) #
deprecated after NX 1.11
g.add_edge(elrow[0], elrow[1], **elrow[2:].to_dict())
app=Tk()
class Trial(tk.Tk):
#self -implied but does not need to be passed at all depending on the
structure
#* = args --> arguments, unlimited number of variables --> can pass
through as many variables as you want
#** = kwargs --> keyboard arguments, passing through dictionaries
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Trial Only")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, plot):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = ttk.Button(self, text="Visit Page 1",
command=lambda: controller.show_frame(PageOne))
button.pack()
#Adding a page
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Back to home",
command=lambda:
controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Visit page two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button3 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button3.pack()
button4 = ttk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button4.pack()
button5 = ttk.Button(self, text="Visit Page 3",
command=lambda: controller.show_frame(PageThree))
button5.pack()
class plot (tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label= tl.Label(self, text= "Figure 1", font = LARGE_FONT)
label.pack(pady=10,padx=10)
# Edge list example
print(elrow[0]) # node1
print(elrow[1]) # node2
print(elrow[2:].to_dict()) # edge attribute dict
# Add node attributes
for i, nlrow in df1.iterrows():
# g.node[nlrow['id']] = nlrow[1:].to_dict() # deprecated after NX 1.11
nx.set_node_attributes(g, {nlrow['ID']: nlrow[1:].to_dict()})
# Node list example
print(nlrow)
# Preview first 5 edges
list(g.edges(data=True))[0:5]
# Preview first 10 nodes
list(g.nodes(data=True))[0:10]
print('# of edges: {}'.format(g.number_of_edges()))
print('# of nodes: {}'.format(g.number_of_nodes()))
# Define node positions data structure (dict) for plotting
for node in g.nodes(data=True):
print(node)
print("")
node_positions = {node[0]: (node[1]['X'], -node[1]['Y']) for node in
g.nodes(data=True)}
# Preview of node_positions
dict(list(node_positions.items())[0:5])
# Define data structure (list) of edge colors for plotting
# edge_colors = [e[2]['color'] for e in g.edges(data=True)]
edge_colors = [e[2]['color'] for e in list(g.edges(data=True))]
# Preview first 10
edge_colors[0:10]
fig = plt.figure(figsize=(8, 6))
nx.draw(g, pos=node_positions, edge_color=edge_colors, node_size=10,
node_color='black')
plt.title('Graph Representation of repair trail', size=15)
canvas = FigureCanvasTkAgg(fig, app)
canvas.get_tk_widget().pack()
canvas.draw()
app = Trial()
app.mainloop()
I want to display one window from tkinter to display all the different pages, however, the output gives me 2 different windows from tkinter
1st with all the starting pages and the buttons
2nd just with the graph from class plot
You get two windows because you create a first window with app = Tk(), then a second one when you init Trial because it inherits from Tk. In fact you don't need app = Tk().
You want your plot to be in one of the pages, so you need to move all the code creating the matplotlib figure inside one of your Page class, e.g. PageTwo:
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two")
label.pack(pady=10, padx=10)
# code for the plot
fig = plt.figure(figsize=(8, 6)) # create matplotlib figure
# add axes and plot (replace this by your matplotlib code)
ax = fig.subplots()
ax.plot(range(10))
ax.set_title('Graph Representation of repair trail', size=15)
# create the tkinter widget to display the figure
canvas = FigureCanvasTkAgg(fig, self) # here self is the widget in which you want to display the figure
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2Tk(canvas, self) # add toolbar
canvas.draw() # show content
# navigation button
button3 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button3.pack()
button4 = ttk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button4.pack()
You are explicitly creating two windows.
The first is this line: app=Tk()
The second is the line app = Trial(), since Trial inherits from tk.Tk.

How to update matplotlib embedded into tkinter?

The problem is that I want to draw a plot by clicking on a button but it doesn't work. However, when I call draw from __init__, the plot appears on the screen.
Plotter.py
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class Plotter(FigureCanvasTkAgg):
def __init__(self, master):
self.figure = Figure(dpi=100)
super().__init__(self.figure, master=master)
self.axes = self.figure.add_subplot(111)
self.get_tk_widget().grid(column=0, row=0, sticky='nsew')
def draw(self):
self.axes.clear()
x_list = [x for x in range(0, 100)]
y_list = [x^3 for x in x_list]
self.axes.plot(x_list, y_list, color='y')
MainApplication.py
from tkinter import ttk
import tkinter as tk
import plotter
class MainApplication(ttk.Frame):
def __init__(self, master, *args, **kwargs):
super().__init__(root)
self.grid(column=0, row=0, sticky='nsew')
frame = ttk.Frame(self, borderwidth=8)
frame.grid(column=0, row=0, sticky='nsew')
frame.rowconfigure(0, weight=1)
notes = ttk.Notebook(frame)
notes.grid(column=0, row=0, sticky='nsew')
notes.rowconfigure(0, weight=1)
page = ttk.Frame(notes)
notes.add(page, text='Picture')
plot = plotter.Plotter(page)
# plot.draw() # This call updates the plot
input_frame = ttk.Frame(self)
input_frame.grid(column=1, row=0, sticky='nsew')
# this binding doesn't update the plot
button = ttk.Button(input_frame, text='Plot', \
command=lambda: plot.draw())
button.grid(column=0, row=4, columnspan=2, sticky='ew')
root = tk.Tk()
MainApplication(root)
root.mainloop()
Personally I would write this up in a single class so that we can use class attributes and methods to control everything with ease. Also you do not need a lambda here. Just save the reference to the command button and not a lambda call. That said you were also overwriting the draw method of FigureCanvasTkAgg so change the draw() method to something else.
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import ttk
import tkinter as tk
class MainApplication(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
notes = ttk.Notebook(self)
notes.grid(column=0, row=0, sticky='nsew')
notes.rowconfigure(0, weight=1)
self.page = ttk.Frame(notes)
notes.add(self.page, text='Picture')
self.plotter()
input_frame = ttk.Frame(self)
input_frame.grid(column=1, row=0, sticky='nsew')
button = ttk.Button(input_frame, text='Plot', command=self.new_draw)
button.grid(column=0, row=4, columnspan=2, sticky='ew')
def plotter(self):
self.figure = Figure(dpi=100)
self.plot_canvas = FigureCanvasTkAgg(self.figure, self.page)
self.axes = self.figure.add_subplot(111)
self.plot_canvas.get_tk_widget().grid(column=0, row=0, sticky='nsew')
def new_draw(self):
self.axes.clear()
x_list = [x for x in range(0, 100)]
y_list = [x^3 for x in x_list]
self.axes.plot(x_list, y_list, color='y')
self.plot_canvas.draw_idle()
MainApplication().mainloop()
You overwrote the canvas' draw method without reimplementing it. But since you do not want to update your plot on every draw-event anyways, I'd suggest to call the method to update the plot differently, e.g. draw_lists. Inside draw_lists you would then need to call the draw method of the canvas (or in this case better draw_idle).
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class Plotter(FigureCanvasTkAgg):
def __init__(self, master):
self.figure = Figure(dpi=100)
super().__init__(self.figure, master=master)
self.axes = self.figure.add_subplot(111)
self.get_tk_widget().grid(column=0, row=0, sticky='nsew')
def draw_lists(self):
self.axes.clear()
x_list = [x for x in range(0, 100)]
y_list = [x^3 for x in x_list]
self.axes.plot(x_list, y_list, color='y')
self.draw_idle()
from tkinter import ttk
import tkinter as tk
class MainApplication(ttk.Frame):
def __init__(self, master, *args, **kwargs):
super().__init__(root)
self.grid(column=0, row=0, sticky='nsew')
frame = ttk.Frame(self, borderwidth=8)
frame.grid(column=0, row=0, sticky='nsew')
frame.rowconfigure(0, weight=1)
notes = ttk.Notebook(frame)
notes.grid(column=0, row=0, sticky='nsew')
notes.rowconfigure(0, weight=1)
page = ttk.Frame(notes)
notes.add(page, text='Picture')
plot = Plotter(page)
input_frame = ttk.Frame(self)
input_frame.grid(column=1, row=0, sticky='nsew')
# this binding doesn't update the plot
button = ttk.Button(input_frame, text='Plot', \
command=lambda: plot.draw_lists())
button.grid(column=0, row=4, columnspan=2, sticky='ew')
root = tk.Tk()
MainApplication(root)
root.mainloop()

Tkinter Serial data on multiple pages

I am using Python3 with TKinter and have run into an issue, even after reading the forums and the TKdocs website I am still making no headway. I am receiving temperature readings via my com-port. My program so far has a Start page and a page one with a graph on it that updates with every reading. So the question is how can I print the sensor data on page one as well, I am new to tkinter.
I will post the code below any advice welcome.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import random
import sys
import time
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
import urllib
import json
import serial # import Serial Library
import numpy # Import numpy
import pandas as pd
import numpy as np
LARGE_FONT= ("Verdana", 12)
style.use("ggplot") #ggplot...dark_background
do = []
tempF= []
f = Figure(figsize=(10,6), dpi=100)
a = f.add_subplot(111)
arduinoData = serial.Serial('com3', 115200) #Creating our serial object
def animate(i):
if(arduinoData.inWaiting()>0):
#read serial data
arduinoString = arduinoData.readline()
xList = []
yList = []
#Parse serial data
arduinoString.split()
['', '', '', '', '', '', '', '']
words = arduinoString.split()
reading = words[3]
if words[1] == (b'TEMP') :
print (words[0])
print (words[1])
print (words[3])
tempF.append(reading) #Build our tempF array by appending temp readings
a.clear()
a.plot(*yList, *yList)
title = " D.O : "+str(do) + "\n Temp : " + str(tempF)
a.set_title(title)
arduinoData.flushInput()
arduinoData.flushOutput()
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Stylibleue Dashboard")
# the container is where we'll stack a bunch of frames on top each other
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
#Switch through pages
self.frames = {}
for F in (StartPage, Page1,):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Page Labels
label = tk.Label(self, text=(""" D.O :
"""), font=LARGE_FONT)
label.grid(row=100, column=20, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>
"""), font=LARGE_FONT)
label.grid(row=100, column=30, sticky="nsew")
label = tk.Label(self, text=(""" TEMP :
"""), font=LARGE_FONT)
label.grid(row=100, column=40, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>
"""), font=LARGE_FONT)
label.grid(row=100, column=50, sticky="nsew")
#Go to Page1 button
button1 = ttk.Button(self, text="Page1",
command=lambda: controller.show_frame(Page1))
button1.grid(row=100, column=60, sticky="nsew")
class Page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Bassin 2!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
#Return home button
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
#This is the embedded matplotlib graph
canvas = FigureCanvasTkAgg(f, self)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
app = Application()
ani = animation.FuncAnimation(f, animate, interval=1000)
app.mainloop()
At first I misunderstood the question, so now I am rewriting the answer. If you still have some doubts or this is not what you were expecting, just comment below. I'll try my best to help. Also, I don't have an arduino to check this for.
I have made the following changes to your code:
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Page Labels
label = tk.Label(self, text=(""" D.O :
"""), font=LARGE_FONT)
label.grid(row=100, column=20, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>
"""), font=LARGE_FONT)
label.grid(row=100, column=30, sticky="nsew")
label = tk.Label(self, text=(""" TEMP :
"""), font=LARGE_FONT)
label.grid(row=100, column=40, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>
"""), font=LARGE_FONT)
label.grid(row=100, column=50, sticky="nsew")
# Reading data from the arduino
def DataRead():
msg = arduinoData.read(arduinoData.inWaiting()) # read everything in the input buffer
print ("Message from arduino: ")
print (msg)
button1 = ttk.Button(self, text="Print arduino data",
command=lambda: DataRead())
button1.grid()
#Go to Page1 button
button1 = ttk.Button(self, text="Page1",
command=lambda: controller.show_frame(Page1))
button1.grid(row=100, column=60, sticky="nsew")

_tkinter.TclError: unknown option "-menu"

I am trying to write the object-oriented code in Python. Now I am stuck with an error:
_tkinter.TclError: unknown option "-menu"
I think the error lies in following line:
self.config(menu=menubar)
Following is my code:
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import time
LARGE_FONT = ("Verdana", 12)
class ImgComp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self,width=320, height=209)
container.pack(side="top")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
container.grid_propagate(False)
self.frames = {}
for F in (PageOne):
frame = F(container, self)
#frame['bg'] = 'red'
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(PageOne)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
menubar = tk.Menu(self)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu.add_command(label="Open")
menubar.add_cascade(label="Settings", menu=filemenu)
self.config(menu=menubar)
label = tk.Label(self, text="Page One", font=LARGE_FONT)
label.pack(pady=10,padx=10)
app = ImgComp()
app.mainloop()
Also I would like to inform, I have truncated the code as I was not allowed to post a bigger code by stackoverflow. Please help.

Resources