python with tkinter

Updated on April 14, 2017 in [A] GUI
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on April 14, 2017

Hi all

Can anyone help me please. I have to implement a droplist menu in the following code:

import matplotlib.pyplot as plt
import pandas as pd
import tkinter as tk
from tkinter import ttk
from tkinter import *
import tkinter

class Application (Frame):
# Read the data into a pandas DataFrame.
cijferset = pd.read_csv(“C:/Users/Anoniem/Desktop/Map1.csv”)

# These are the “Tableau 20” colors as RGB.
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]

# Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
for i in range(len(tableau20)):
r, g, b = tableau20[i]
tableau20[i] = (r / 255., g / 255., b / 255.)

# You typically want your plot to be ~1.33x wider than tall. This plot is a rare
# exception because of the number of lines being plotted on it.
# Common sizes: (10, 7.5) and (12, 9)
plt.figure(figsize=(12, 9))

# Remove the plot frame lines. They are unnecessary chartjunk.
ax = plt.subplot(111)

# Ensure that the axis ticks only show up on the bottom and left of the plot.
# Ticks on the right and top of the plot are generally unnecessary chartjunk.

# Limit the range of the plot to only where the data is.
# Avoid unnecessary whitespace.
plt.ylim(0, 30)
plt.xlim(2006, 2011)

# Make sure your axis ticks are large enough to be easily read.
# You don’t want your viewers squinting to read your plot.
plt.yticks(range(0, 26, 5), [str(x) + “%” for x in range(0, 26, 5)], fontsize=14)

# Provide tick lines across the plot to help your viewers trace along
# the axis ticks. Make sure that the lines are light and small so they
# don’t obscure the primary data lines.
for y in range(0, 26, 1):
plt.plot(range(1968, 2012), [y] * len(range(1968, 2012)), “–“, lw=0.5, color=”black”, alpha=0.3)

# Remove the tick marks; they are unnecessary with the tick lines we just plotted.
plt.tick_params(axis=”both”, which=”both”, bottom=”off”, top=”off”,
labelbottom=”on”, left=”off”, right=”off”, labelleft=”on”)

# Now that the plot is prepared, it’s time to actually plot the data!
# Note that I plotted the majors in order of the highest % in the final year.
deelgemeentes = [‘Charlois’, ‘Delfshaven’, ‘Feijenoord’, ‘Hillegersberg-Schiebroek’, ‘Hoek van Holland’, ‘Hoogvliet’,
‘IJsselmonde’, ‘Kralingen-Crooswijk’, ‘Noord’, ‘Overschie’, ‘Pernis’, ‘Prins Alexander’, ‘Stadscentrum’, ‘Rotterdam’]

for rank, column in enumerate(deelgemeentes):
# Plot each line separately with its own color, using the Tableau 20
# color set in order.
cijferset[column.replace(“\n”, ” “)].values,
lw=2.5, color=tableau20[rank])

# Add a text label to the right end of every line. Most of the code below
# is adding specific offsets y position because some labels overlapped.
y_pos = cijferset[column.replace(“\n”, ” “)].values[-1] – 0.5
if column == “Charlois”:
y_pos += 12.0
elif column == “Delfshaven”:
y_pos += 10.0
elif column == “Feijenoord”:
y_pos += 10.0
elif column == “Hillegersberg-Schiebroek”:
y_pos += 13.0
elif column == “Hoek van Holland”:
y_pos += 14.25
elif column == “Hoogvliet”:
y_pos += 10.25
elif column == “IJsselmonde”:
y_pos += 6.5
elif column == “Kralingen-Crooswijk”:
y_pos += 4.5
elif column == “Noord”:
y_pos += 2.5
elif column == “Overschie”:
y_pos += 3.0
elif column == “Pernis”:
y_pos += 4.5
elif column == “Prins Alexander”:
y_pos += 4.0
elif column == “Stadscentrum”:
y_pos -= 1.75
elif column == “Rotterdam”:
y_pos -= 1.75

# Again, make sure that all labels are large enough to be easily read
# by the viewer.
plt.text(2011.05, y_pos, column, fontsize=10, color=tableau20[rank])

# matplotlib’s title() call centers the title on the plot, but not the graph,
# so I used the text() call to customize where the title goes.

# Make the title big enough so it spans the entire plot, but don’t make it
# so big that it requires two lines to show.

# Note that if the title is descriptive enough, it is unnecessary to include
# axis labels; they are self-evident, in this plot’s case.
plt.text(2009, 28, “Diefstal per deelgemeente (2006-2011)”, fontsize=17, ha=”center”)

# Always include your data source(s) and copyright notice! And for your
# data sources, tell your viewers exactly where the data came from,
# preferably with a direct link to the data. Just telling your viewers
# that you used data from the “U.S. Census Bureau” is completely useless:
# the U.S. Census Bureau provides all kinds of data, so how are your
# viewers supposed to know which data set you used?
plt.text(2006, -3, “Data source:”
“\nAuthor: Projectgroup 5 “, fontsize=10)

# Finally, save the figure as a PNG.
# You can also save it as a PDF, JPEG, etc.
# Just change the file extension in this call.
# bbox_inches=”tight” removes all the extra whitespace on the edges of your plot.
plt.savefig(“Diefstal2006-2011.png”, bbox_inches=”tight”)

I have to implement a dropdownlist. So a several graphs with data. But I will make a button when I click on it, the grapgh will apears and so on with other graphs. I think I can use the following code for the dropdownlist and implement it to the above code.

class Application (Frame):
# Read the data into a pandas DataFrame.

def __init__(self,master):
#Initialize the frame
ttk.Frame.__init__(self, master)

def create_widgets(self):
# create the window layout
myFont = (‘Lucida Grande’,12)
#create Label symbol
self.label2 = Label(self, font=myFont, text =”symbol”).grid(row=0,column=0)
#create combobox for the symbol
self.cbSymbol = ttk.Combobox(self, font=myFont, width=7,textvariable=varSymbol)
self.cbSymbol.bind(“<Return>”, self.cbSymbol_onEnter) # when the enter key is press an event happens
self.cbSymbol[‘values’] = (‘AAPl’,’FB’,’NFLX’)
self.cbSymbol.grid(row=2,column=1,sticky = W)
def cbSymbol_onEnter(self, event):
#changes characters to upper case
#gets the value of the text in the combox. cbSymbol
mytext = varSymbol.get()
#gets list of values from dropdown list of
#cbSymbol combobox
vals = self.cbSymbol.get(‘values’)
#selects all in the combobox. cbSymbol
self.cbSymbol.select_range(0, END)
#checks of the symbol exist in the combobox if not add it
#to the dropdown list
if not vals:
self.cbSymbol.configure(values = (mytext,))
elif mytext not in vals:
self.cbSymbol.configure(values = vals+(mytext, ))
return ‘break’
root = Tk()
varSymbol = StringVar (root, value=’NFLX’)
app = Application(root)


Can anyone help me please?

  • Liked by