954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python and Tkinter canvas and buttons problem

Hi,

I'm a relative newb to Python and I'm starting my first major project - a logarithmic graphing system. I'm using tkinter for the GUI part of the design, but I've run into a problem. Basically, I want my graph (a tk canvas) to appear in the same (root) window as the buttons that control it. How do I go about this? Any help would be greatly appreciated. Thanks in advance :)

# plot of log line

from tkinter import *
import math

def particleCount(decay, p0, time):
        p = (p0*math.e)**(-(decay*time))
        return (p)

# define root window
root = Tk()
root.title("Radioactive Decay Graph")

# set canvas properties
width = 400
height = 400
center = height*(5/6)
x_increment = 1

# invoke canvas
c = Canvas(width=width, height=height, bg='black')
c.pack()
# add string
str1 = "Logarithmic decay graph"
c.create_text(10, 20, anchor=SW, text=str1)

#       line width stretch
x_factor = 1
#       line height stretch
y_amplitude = -300
center_line = c.create_line(0, center +1, width, center+1, fill='green')

coordinates = []
coord = []
for x in range(0, 600):
    # x coordinates
    coord.append((x * x_increment) * x_factor)

    # y coordinates
    y = particleCount((1.16*(10**-3)), (5*(10**6)), x)
    coord.append((y * y_amplitude) + center)

    coordinates.append(coord)
    coord = []
    
    
log_line = c.create_line(coordinates, fill='red')

root.mainloop()
CharlieNewey
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Something like this?

# plot of log line

from tkinter import *
import math

def particleCount(decay, p0, time):
        p = (p0*math.e)**(-(decay*time))
        return (p)

# define root window
root = Tk()
root.title("Radioactive Decay Graph")

# create frame to put control buttons onto
frame = Frame(root, bg='grey', width=400, height=40)
frame.pack(fill='x')
button1 = Button(frame, text='Button1')
button1.pack(side='left', padx=10)
button2 = Button(frame, text='Button2')
button2.pack(side='left')

# set canvas properties
width = 400
height = 400
center = height*(5/6)
x_increment = 1

# invoke canvas
c = Canvas(root, width=width, height=height, bg='black')
c.pack()
# add string
str1 = "Logarithmic decay graph"
c.create_text(10, 20, anchor=SW, text=str1)

#       line width stretch
x_factor = 1
#       line height stretch
y_amplitude = -300
center_line = c.create_line(0, center +1, width, center+1, fill='green')

coordinates = []
coord = []
for x in range(0, 600):
    # x coordinates
    coord.append((x * x_increment) * x_factor)

    # y coordinates
    y = particleCount((1.16*(10**-3)), (5*(10**6)), x)
    coord.append((y * y_amplitude) + center)

    coordinates.append(coord)
    coord = []


log_line = c.create_line(coordinates, fill='red')

root.mainloop()
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Perfect, a little tweak to that and you've given me just what I needed. Thanks a lot :)

CharlieNewey
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: