-1

I have the button which have to show my graph by pressing. But it shows immediately when i run my programm. It works normaly if my parameters left and height for graph contains in the click_graph fuction like this.

def click_graph():    
    left = [1, 2, 3, 4, 5]
    height = [10, 24, 36, 40, 5]
    plt.bar(left, height, width = 1, color = ['grey', 'black'])
    plt.show()

But I want to do like this. (Full program)

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

def click_graph(left, height):    
    plt.bar(left, height, width = 1, color = ['grey', 'black'])
    plt.show()

def win(left, height):
    root = tk.Tk()
    button = tk.Button(root, text= "Graph",command=click_graph(left, height))
    button.pack()
    button.place(x=10, y=100)

    root.mainloop()

def main():
    left = [1, 2, 3, 4, 5]
    height = [10, 24, 36, 40, 5]
    #opening window to press the button to show graph
    win(left, height)

if __name__ == '__main__':
    main()

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • You're not binding the `click_graph` function to the `command` callback. You're binding the result of `click_graph`, since you're explicitly invoking it. Try `command=lambda l=left, h=height: click_graph(l, h)` – Paul M. Feb 15 '23 at 18:59

1 Answers1

0

Now it works.

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

left = []
height = []

def click_graph():    
    plt.bar(left, height, width = 1, color = ['grey', 'black'])
    plt.show()

def win(left, height):
    root = tk.Tk()
    button = tk.Button(root, text= "Graph",command=click_graph)
    button.pack()
    button.place(x=10, y=100)

    root.mainloop()

def main():
    global left, height
    left = [1, 2, 3, 4, 5]
    height = [10, 24, 36, 40, 5]
    #opening window to press the button to show graph
    win(left, height)

if __name__ == '__main__':
    main()