第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

matplotlib PolygonSelector 在 tkinter 中調(diào)用時(shí)凍結(jié)

matplotlib PolygonSelector 在 tkinter 中調(diào)用時(shí)凍結(jié)

胡子哥哥 2021-09-28 13:24:58
我正在使用 tkinter 和 matplotlib 編寫腳本進(jìn)行數(shù)據(jù)處理,代碼的某些部分需要多邊形選擇器來(lái)選擇感興趣的區(qū)域。但是,PolygonSelector 無(wú)法檢測(cè)光標(biāo)的運(yùn)動(dòng)。需要注意的是,這個(gè)問題是在matplotlib圖形交互模式開啟的情況下出現(xiàn)的。簡(jiǎn)化代碼和結(jié)果如下所示:#!/usr/bin/env python3import matplotlibmatplotlib.use("TkAgg")import tkinter as tkimport matplotlib.pyplot as pltfrom matplotlib.widgets import PolygonSelectorroot = tk.Tk()def draw():    fig = plt.figure()    ax = fig.add_subplot(111)    plt.ion()    # interactive mode is on    plt.show()    def onselect(data_input):        print(data_input)    PS = PolygonSelector(ax, onselect)tk.Button(root, text='draw', command=draw).pack()root.mainloop()這是在 tkinter GUI 上單擊“繪制”按鈕后的圖,多邊形的起點(diǎn)停留在 (0,0),預(yù)計(jì)會(huì)隨光標(biāo)移動(dòng):當(dāng)我draw()在 tkinter 之外調(diào)用時(shí),PolygonSelector 工作正常:def draw():    fig = plt.figure()    ax = fig.add_subplot(111)    plt.ion()    # interactive mode is on    plt.show()    def onselect(data_input):        print(data_input)    PS = PolygonSelector(ax, onselect)    a = input()    # prevent window from closing when execution is donedraw()
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

簡(jiǎn)單的解決方案是確保您將多邊形選擇器設(shè)為全局變量。這將使選擇器在視覺上保持更新。


#!/usr/bin/env python3

import tkinter as tk

import matplotlib

import matplotlib.pyplot as plt

from matplotlib.widgets import PolygonSelector

matplotlib.use("TkAgg")



root = tk.Tk()

ps = None


def draw():

    global ps

    fig = plt.figure()

    ax = fig.add_subplot(111)

    plt.ion()

    plt.show()

    ps = PolygonSelector(ax, on_select)



def on_select(data_input):

    print(data_input)


tk.Button(root, text='draw', command=draw).pack()

root.mainloop()

如果將其構(gòu)建到類中,則可以避免使用 global 并通過將 Polygon Selector 作為類屬性應(yīng)用來(lái)獲得所需的行為。


#!/usr/bin/env python3

import tkinter as tk

import matplotlib

import matplotlib.pyplot as plt

from matplotlib.widgets import PolygonSelector

matplotlib.use("TkAgg")



class GUI(tk.Tk):

    def __init__(self):

        super().__init__()

        self.ps = None

        tk.Button(self, text='draw', command=self.draw).pack()


    def draw(self):

        fig = plt.figure()

        ax = fig.add_subplot(111)

        plt.ion()

        plt.show()

        self.ps = PolygonSelector(ax, self.on_select)


    def on_select(self, data_input):

        print(data_input)



if __name__ == "__main__":

    GUI().mainloop()

結(jié)果:

查看完整回答
反對(duì) 回復(fù) 2021-09-28
  • 1 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)