简体   繁体   English

Python 3.4使鼠标单击事件前进到数据列表

[英]Python 3.4 Make mouse click event advance through a list of data

I have a nested list of data (three dimensional) and I have written a program to give me a generic analysis of the data, and report it graphically. 我有一个嵌套的数据列表(三维),并且编写了一个程序,可以对数据进行常规分析并以图形方式报告。

I would like to work through these graphs with a push of a TKINTER button. 我想通过按TKINTER按钮浏览这些图形。

I have assigned the button a command=NextDataSet and have a tkinter tk.mainloop() . 我已经为按钮分配了一个command=NextDataSet并有一个tk.mainloop() There exists a graphData() function that works pretty well. 有一个graphData()函数可以很好地工作。

However, writing NextDataSet has turned into a confusing experience. 但是,编写NextDataSet变成了令人困惑的体验。

I have thought about running through each item in the main list, but I want it to stop each time. 我曾考虑过遍历主列表中的每个项目,但我希望它每次都停止。 Hence, I have not put a loop in the command. 因此,我没有在命令中放入循环。

 def NextDataSet(): 
     for eachDataSet in BigList:
         graphData(eachDataSet)

Any thoughts how to display the next dataset in the list, one by one, with a tkinter mouse button event? 有什么想法如何使用tkinter鼠标按钮事件在列表中一个接一个地显示下一个数据集? I have considered passing parameters back and forth... 我已经考虑过来回传递参数...

Getting the next dataset can be done with a simple counter. 可以使用一个简单的计数器来获取下一个数据集。 Note that CamelCaseNames are used for classes. 请注意,CamelCaseNames用于类。 Functions use all lower_case_with_underscores. 函数使用所有lower_case_with_underscores。 It helps others to read and understand your code. 它可以帮助其他人阅读和理解您的代码。

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

class CycleThroughClass(object):
    def __init__(self, master):
        self.display_1=tk.Label(master, bg="lightblue")
        self.display_1.grid(row=0, column=0, sticky="nsew")
        self.display_2=tk.Label(master, bg="lightgreen")
        self.display_2.grid(row=0, column=1, sticky="nsew")

        tk.Button(master, text="next item", bg="khaki",
                  command=self.next_data_set).grid(row=1,
                  column=0, sticky="nsew")

        tk.Button(master, text="Quit", bg="orange",
                  command=master.quit).grid(row=2, column=0, sticky="nsew")

        self.ctr=0
        self.questions=[["one", 1], ["two", 2], ["three", 3],
                        ["four", 4], ["five", 5]]
        self.next_data_set()

    def next_data_set(self):
        if self.ctr < len(self.questions):
            question, number=self.questions[self.ctr]
            self.display_1.config(text=question)
            self.display_2.config(text=str(number))
            self.ctr += 1
        else:
            self.display_1.config(text="Sorry, no more")
            self.display_2.config(text="")


master=tk.Tk()
CT=CycleThroughClass(master)
master.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM