如何将绘制的图放置在tkinter窗口上的特定位置

时间:2019-04-09 11:57:12

标签: python matplotlib arduino tkinter-canvas

我是python编程的新手,我面临的问题是在绘制图形时我无法将图形放置在所需的位置。我可以使用tkinter的place()方法将按钮放置在tkinter窗口中我喜欢的任何位置。

这是我的家庭项目。在下面的代码中,我制作了两个按钮,其中的“ CONNECT”按钮将连接至已插入的arduino,并且plot按钮将获取来自arduino的数据并将其绘制,并且由于它是测试代码,因此我在其中输入了随机值。

### Author = Peouse Dutta

import tkinter as tk
from tkinter import ttk
import serial
import serial.tools.list_ports
import time
import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt


root = tk.Tk()
root.title("ANALYSER")
root.geometry("1000x700")
root.configure(background = 'white')


def get_ports():
    ports = serial.tools.list_ports.comports()
    return ports

def findArduino(portsFound):
    commPort = 'None'
    numConnection = len(portsFound)

    for i in range(0,numConnection):
        port = portsFound[i]
        strPort = str(port)

        if 'Arduino' in strPort:
            splitPort = strPort.split(' ')
            commPort = (splitPort[0])

    return commPort


class AppWindow(tk.Tk):
    def __init__(self, window):
        self.window = window
        self.connect = ttk.Button(window, text = "CONNECT", command = self.Connect)
        self.connect.pack()
        self.getValue = ttk.Button(window, text = "PLOT", command = self.plot)
        self.getValue.pack()


    def Connect(self):
        self.FoundPorts = get_ports()
        self.connectPort = findArduino(self.FoundPorts)
        if self.connectPort != 'None':
            ser = serial.Serial(self.connectPort, baudrate = 9600)
            #time.sleep(1.5)
            print('Connected to ' + self.connectPort)
            tk.messagebox.showinfo('STATUS', 'CONNECTED')
        else:
            print('Connection Issue!')
            tk.messagebox.showinfo('STATUS', 'NOT CONNECTED!')

    def plot(self):
        fig = plt.figure(figsize = (10,2), dpi = 100)
        a = fig.add_subplot(111)
        a.plot([1,1,1,1,2,3,3,4],[8,9,5,6,2,2,2,2])

        canvas = FigureCanvasTkAgg(fig, master = self.window)
        fig.canvas.draw()
        canvas.get_tk_widget().pack(side = tk.TOP, fill = tk.Y)




start = AppWindow(root)
root.mainloop()

我希望绘制的图形像这样。 我提供了几个例子

enter image description here

后来我会喜欢的

enter image description here

0 个答案:

没有答案