用python创建一个数学矩阵

时间:2014-07-28 05:35:54

标签: python math matrix tkinter scrollbar

嗯,在问之前我在网上看了很多,所以这就是事情。 我没有Python的经验,我需要开发这个Matrix Generator。是的,这是一个数学矩阵。我使用的是Tkinter和Python 3.3。 首先,我询问行数和列数,然后填写每个字段,代码将其转换为记事本以供其他程序阅读。

我使用了网格方法,因此我遇到了大规模矩阵的问题。问题是我需要应用滚动条。我已经读过我可以使用框架,我的一个朋友向我展示了他使用框架制作的滚动条。你能帮助我吗? ; d

每次我引用linhas都与行相同,colunas与列相同。

import tkinter

class DropDown:
    def __init__(self):


        self._list_window = tkinter.Tk();

        self._row_var = tkinter.StringVar(self._list_window)
        self._col_var = tkinter.StringVar(self._list_window)
        self._row_var.set(0)
        self._col_var.set(0)

        self._rows = None
        self._columns = None

        self._row_label = tkinter.Label(
            master = self._list_window,
            text = 'Number of rows: ')

        self._row_label.grid(
            row = 0, column = 0, padx = 5, pady = 5, 
            sticky = tkinter.NE)

        self._row_entry = tkinter.Entry(self._list_window, width=1)

        self._row_entry.grid(
            row = 0, column = 1, padx = 5, pady = 5,
            sticky = tkinter.EW)

        self._column_label = tkinter.Label(
            master = self._list_window,
            text = 'Number of columns: ')

        self._column_label.grid(
            row = 1, column = 0, padx = 5, pady = 5, 
            sticky = tkinter.NE)

        self._column_entry = tkinter.Entry(self._list_window)

        self._column_entry.grid(
            row = 1, column = 1, padx = 5, pady = 5, 
            sticky = tkinter.EW)

        self._list_window.columnconfigure(1, weight = 1)

        self._OK_button = tkinter.Button(
            master = self._list_window, text = "OK",
            command = self.get_dimensions)

        self._OK_button.grid(
            row = 2, column = 0, columnspan = 2,
            padx = 5, pady = 5)

    def get_dimensions(self):
        self._rows = self._row_entry.get()
        self._columns = self._column_entry.get()
        self._list_window.destroy()

    def show(self):
        self._list_window.mainloop()
        if self._rows != None and self._columns != None:
            return (int(self._rows), int(self._columns))
        else:
            return (None, None)

class matrix:
    def __init__(self, linhas, colunas):
        self.linhas=linhas
        self.colunas=colunas
        self.mwindow=tkinter.Tk()
        self.vars = [[] for x in range(self.linhas) ]

        for i in range(self.linhas):
            for j in range(self.colunas):
                v=tkinter.StringVar()
                self.vars[i].append(v)
                entry = tkinter.Entry(master = self.mwindow, textvariable = self.vars[i][j])
                entry.grid(row = i, column = j, padx = 10, pady = 10)

        self.botOK=tkinter.Button(master=self.mwindow, text="OK", command=self.OK)
        self.botOK.grid(row=self.linhas+1, column=(self.colunas//2)-1, columnspan=2, padx=5, pady=5)       

    def Start(self):
        self.mwindow.mainloop()            
        return self.lista

    def OK(self):
        self.lista = []
        for i in range(self.linhas):
            for j in range(self.colunas):
                self.lista.append(self.vars[i][j].get())

        self.mwindow.destroy()



dimensoes= DropDown().show()
#print (dimensoes)
if dimensoes[0]<dimensoes[1]:
    diag=dimensoes[0]
else:
    diag=dimensoes[1]
matriz=matrix(dimensoes[0], dimensoes[1]).Start()

with (open("notepadmatrix.txt", "w")) as arquivo:
    arquivo.write(str(dimensoes[0]*dimensoes[1])+"\n")
    arquivo.write(str(dimensoes[0])+"\n")
    arquivo.write(str(dimensoes[1])+"\n")
    arquivo.write(str(diag)+"\n")
    for i in matriz:
        arquivo.write(i+"\n")
    arquivo.write("fim")

这里是他给我滚动条的代码。

class Earnings():

    def __init__(self, Ticker, EPS, Time):
        self._root_window = tkinter.Tk()

        self.canvas = tkinter.Canvas(master = self._root_window, background = '#8989E0')
        self.canvas.config(scrollregion=[0,0,600,10000])

        self.frame = tkinter.Frame(master = self.canvas)

        self.scrollbar = tkinter.Scrollbar(master = self._root_window, orient = tkinter.VERTICAL)

        self.frame.pack(side = tkinter.LEFT, fill = tkinter.BOTH, expand = tkinter.TRUE)
        self.canvas.create_window((0,0), window=self.frame, anchor=tkinter.NW)
        self.scrollbar.pack(side = tkinter.RIGHT, fill = tkinter.BOTH, expand = tkinter.TRUE) 

        self.canvas.pack(side = tkinter.TOP, fill = tkinter.BOTH, expand = tkinter.TRUE)

        self.scrollbar.config(command = self.canvas.yview)
        self.canvas.config(yscrollcommand = self.scrollbar.set)

        self.ticker = Ticker
        self.EPS = EPS
        self.time = Time

        for i in range(len(self.ticker)):

            self.TickerButton = tkinter.Button(
                master = self.frame,
                text = self.ticker[i],
                command = lambda i=i: self.search_ticker(self.ticker[i]))

            self.TickerButton.grid(row = i+1, column = 0, padx = 10, pady = 10,
                                  sticky = tkinter.W)

            self.EPSLabel = tkinter.Label(
                master = self.frame,
                text = self.EPS[i])

            self.EPSLabel.grid(row = i+1, column = 1, padx = 10, pady = 10,
                                  sticky = tkinter.W)

            self.TimeLabel = tkinter.Label(
                master = self.frame,
                text = self.time[i])

            self.TimeLabel.grid(row = i+1, column = 2, padx = 10, pady = 10,
                                  sticky = tkinter.W)

1 个答案:

答案 0 :(得分:1)

TkInter很长,我使用的是Kivy Grid布局:

http://kivy.org/docs/api-kivy.uix.gridlayout.html?highlight=grid