Creating an oval inside a pyplot in Tkinter window

Tafla Magnaw haregot21 at gmail.com
Fri Jul 21 13:14:03 EDT 2017


 I spent about a week solving the problem I have in this code but I couldn't do anything. I just give up working due to that line of code.
 
 What I need is that: 
I have a main window with an "oval menu bar" and under the menu-bar, there is a " Add Oval" drop-down menu. when I click the "Add oval" drop-down menu, a pop up window with entry boxes will display.Enter the x & y location (for example 50 &150 and click the send button, the oval should display inside of the left side pyplot. The code below works fine with tkinter window but not working with in the pyplot. How can I create the oval inside the left side of the pyplot? I spent long time to figure out that but I couldn't do anything.I really need any help please.The only problem with the code below is on the App(tk.Tk) class.I really need any help please!! Thank you
 Here is the code I tried:
 
 
 from tkinter import *
 from tkinter import Tk, Frame, Entry, Button, 
 Listbox,StringVar,Label,Menu,filedialog, END
 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
 import tkinter as tk
 from matplotlib.figure import Figure
 import matplotlib.gridspec as gridspec
 root = tk.Tk()
 RADIUS = 10
 root.title("Oval window")
 fig = Figure(figsize=(10,10))
 gs = gridspec.GridSpec(1, 2)
 ax1 = fig.add_subplot(gs[0, 0])
 ax1.set_xticks([])
 ax1.set_xticklabels([])
 ax1.set_yticks([])
 ax1.set_yticklabels
 gs = gridspec.GridSpec(2, 2)
 ax2 = fig.add_subplot(gs[0, 1])
 ax2.set_xticks([])
 ax2.set_xticklabels([])
 ax2.set_yticks([])
 ax2.set_yticklabels
 gs = gridspec.GridSpec(2, 2)
 ax3 = fig.add_subplot(gs[1, 1])
 ax3.set_xticks([])
 ax3.set_xticklabels([])
 ax3.set_yticks([])
 ax3.set_yticklabels
 canvas = FigureCanvasTkAgg(fig, master=root)
 canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
 canvas.get_tk_widget().pack()
 canvas.draw()
 fig.set_tight_layout({'rect': [0, 0, 0.996, 0.998], 'pad': 0.2, 'h_pad': 0.2})
 fig.set_facecolor( 'blue' )
 def Remove_entries():
 for field in fields:
 field.Remove(0,END) 
 def enter_values():
 nw = Toplevel()
 nw.title(" Removing Oval")
 Remove_button = Button(nw, text=" Remove", command=Remove_entries)
 Remove_button.pack() 
 Button1 = Button(nw, text=" Cancel",command=nw.destroy) 
 Button1.pack()
 TheListBox = Listbox(nw) # Add a list entry widget
 TheListBox.pack() 
 for item in ["oval1", "oval2", "oval3", "oval4","SS5","oval6....","ovaln"]: # add four items to the listbox from the main window
 TheListBox.insert(END, item)
 #################
 class Config9(tk.Toplevel):
 def __init__(self, master=None, **kwargs): 
 tk.Toplevel.__init__(self, master, **kwargs) 
 btn = tk.Button(self, text='Send',fg="blue", 
 command=self.add)
 btn.grid(row=8, column=0, sticky=tk.W)
 btn = tk.Button(self, text='Cancel',fg="red", 
 command=self.destroy)
 btn.grid(row=8, column=10, sticky=tk.E)
 lb1 = tk.Label(self, text="Location in (x,y)")
 lb1.grid(row=2, column=0, columnspan=1, sticky=tk.EW)
 self.e1 = tk.Entry(self)
 self.e1.grid(row=2, column=1, columnspan=20, sticky=tk.E)
 self.e1.focus_set()
 lbl2 = tk.Label(self, text=" Type")
 lbl2.grid(row=0, column=0, columnspan=1, sticky=tk.W)
 self.e2 = tk.Entry(self)
 self.e2.grid(row=0, column=1, columnspan=20, sticky=tk.E)
 self.e2.focus_set()
 lbl3 = tk.Label(self, text="SSN")
 lbl3.grid(row=1, column=0, columnspan=1, sticky=tk.W)
 self.e3 = tk.Entry(self)
 self.e3.grid(row=1, column=1, columnspan=20, sticky=tk.E)
 self.e3.focus_set()
 lbl4 = tk.Label(self, text="Oreintation") 
 lbl4.grid(row=3, column=0, columnspan=1, sticky=tk.W)
 self.e4 = tk.Entry(self)
 self.e4.grid(row=3, column=1, columnspan=20, sticky=tk.E)
 self.e4.focus_set()
 lbl5 = tk.Label(self, text="Constant") 
 lbl5.grid(row=4, column=0, columnspan=1, sticky=tk.W)
 self.e5 = tk.Entry(self)
 self.e5.grid(row=4, column=1, columnspan=20, sticky=tk.E)
 self.e5.focus_set()
 self.transient(master) # set to be on top of the main window 
 self.grab_set()
 master.wait_window(self)
 def add(self):
 x, y = self.e1.get().split()
 self.master.place_dot(int(x), int(y)) 
 class App(tk.Tk):
 def __init__(self):
 #### Hello, here is the problem!!!! How can I change this window?
 btn = tk.Button(self, text='Add Oval ',fg="blue", 
 command=self.get_options)
 btn.grid(row=0, column=0, sticky=tk.W)
 btn = tk.Button(self, text='Cancel',fg="red", 
 command=self.destroy)
 btn.grid(row=0, column=1, sticky=tk.E)
 self.can = tk.Canvas(self, width=400, height=400)
 self.can.grid(row=1, column=0, columnspan=2)
 def get_options(self):
 Config9(self)
 def place_dot(self, x, y):
 x1 = x - RADIUS
 y1 = y - RADIUS
 x2 = x + RADIUS
 y2 = y + RADIUS
 self.can.create_oval(x1, y1, x2, y2,fill='green')
 def callback9(self,button):
 if button==" btn":
 print ("btn") 
 ###
 menubar = Menu(root)
 ovalmenu = Menu(menubar)
 ovalmenu.add_command(label="Add oval", command=Config9)
 ovalmenu.add_command(label="Remove oval", command=enter_values)
 menubar.add_cascade(label=" Oval", menu=ovalmenu)
 root.config(menu=menubar)
 root.mainloop()
 win = App()
 win.mainloop()


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /