Sunday, August 27, 2023

FIGURE AND CANVAS IN TKINTER

 


SOURCE CODE:


import tkinter as tk
from tkinter import font
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

class TampilGrafik:
    def __init__(self, root):
        self.root = root
        self.root.geometry(f"{1200}x{550}")
        self.root.title("Menampilkan Grafik")
        self.tambahkan_widgets(root)
        self.entry_fase.bind("<Return>", self.ikat_event)
        self.entry_frek.bind("<Return>", self.ikat_event)

    def tambahkan_widgets(self, root):
        #Menambahkan canvas widget pada root
        self.figure = Figure(figsize=(10, 5), dpi=100)
        self.canvas = FigureCanvasTkAgg(self.figure, master=self.root)
        self.canvas.get_tk_widget().grid(row=0, column=1, columnspan=1, rowspan=25, padx=10, pady=10)

        #Menambahkan button widget pada root
        self.tombol = tk.Button(root, height=3, width=20, text="TAMPILKAN GRAFIK", command=self.tampilkan_grafik)
        self.tombol.grid(row=0, column=0, sticky='n', padx=5, pady=5)

        #Menambahkan label frekuensi
        self.label_freq = tk.Label(root, text="FREKUENSI = ", font=("Heltevica", 14))
        self.label_freq.grid(row=1, column=0, padx = 5, pady = 5, sticky='w')

        #Menambahkan entry frekuensi
        self.entry_frek = tk.Entry(root, width=13, bg="red", fg="white", font=("Heltevica", 14))
        self.entry_frek.grid(row=2, column=0, padx=5, pady=5, sticky='w')                          

        #Menambahkan label fase
        self.label_fase = tk.Label(root, text="FASE = ", font=("Heltevica", 14))
        self.label_fase.grid(row=3, column=0, padx = 5, pady = 5, sticky='w')

        #Menambahkan entry fase
        self.entry_fase = tk.Entry(root, width=13, bg="blue", fg="white", font=("Heltevica", 14))
        self.entry_fase.grid(row=4, column=0, padx=5, pady=5, sticky='w') 


    def baca_frekuensi(self):
        baca = self.entry_frek.get()
        if baca.strip().isdigit():
            frek = float(baca)
        else:
            frek = 5 #nilai default
        return frek

    def baca_fase(self):
        baca = self.entry_fase.get()
        if baca.strip().isdigit():
            fase = float(baca)
        else:
            fase = 5 #nilai default
        return fase

    def tampilkan_grafik(self):
        self.figure.clear()
        self.plot = self.figure.add_subplot(1,1,1)

        #baca frekuensi
        frek = self.baca_frekuensi()

        #baca fase
        fase = self.baca_fase()

        #Grafik sinus dan kosinus
        x = np.linspace(0, 1, 2000)
        #sinus dan cos
        y1 = np.sin(2*np.pi*frek*x + fase)
        self.plot.plot(x, y1, color="blue", label="Sin(x)", linewidth=2)
        y2 = np.cos(2*np.pi*frek*x + fase)
        self.plot.plot(x, y2, color="red", label="Cos(x)", linewidth=2)

        #atribut-atribut grafik
        self.plot.set_title("Grafik sinus dan kosinus dengan frekuensi = " + str(frek) +
                            " dan fase = " + str(fase))
        self.plot.set_xlabel("Waktu")
        self.plot.set_ylabel("Amplitodo")
        self.plot.legend()
        self.plot.grid(True)
        self.plot.set_facecolor("black")
        self.figure.tight_layout()
        self.canvas.draw()

        #Bersihkan kedua entry
        self.entry_fase.delete(0, tk.END)
        self.entry_frek.delete(0, tk.END)

    def ikat_event(self, event):
        self.tampilkan_grafik()

if __name__ == "__main__":
    root=tk.Tk()
    app = TampilGrafik(root)
    root.mainloop()







No comments:

Post a Comment