1

我想制作一个程序,它从用户那里获取一个整数并在 Tkinkter 窗口中输入尽可能多的条目。从这些条目中获取数据并根据它们制作图表。但是当我打开我的程序时,一切都很好,只有一个图表是错误的。它向我显示了一个没有任何数据的空图表,没有标题,也没有“标签名称”。这是我的代码。请帮忙。我正在使用 Python 2.7.5,PyDev for Eclipse

# -*- coding: utf-8 -*-



import matplotlib.pyplot as mp
import Tkinter as T, sys
def end():
    sys.exit()
def check():
    z = e.get()
    try:
        z = int(z)
        e.config(bg = 'green')
        e.after(1000, lambda: e.config(bg = 'white'))

        x = []
        y = []
        global x1, y1
        x1 = []
        y1 = []
        l2 = T.Label(main, text = 'X',bg = 'yellow')
        l2.pack()
        for i in range(0,z):
            x.append(T.Entry(main, justify = 'center'))
            x[i].pack()
            x1.append(x[i].get())
        l3 = T.Label(main, text = 'Y', bg = '#3366ff')
        l3.pack()
        for i in range(0,z):
            y.append(T.Entry(main, justify = 'center'))
            y[i].pack() 
            y1.append(y[i].get())
    except:
        e.config(bg = 'red')
        e.after(1000, lambda: e.config(bg = 'white'))
    return x1,y1


def graph():
    mp.ion()
    mp.plot(x1,y1)  
    mp.title('Wykres')
    mp.xlabel('x')
    mp.ylabel('y')
    mp.draw()






#====================================================================#

y1 = []
x1 = []
z = 0
main = T.Tk()
main.title('GRAPH')
main.geometry('600x600')
main.config(bg = "#3366ff")

e = T.Entry(main,justify = 'center')
l = T.Label(main,text = 'Podaj liczbę parametrów N =',bg = '#3366ff')
b1 = T.Button(main, text = 'OK', command = check)

b2 = T.Button(main, text = 'Rysuj', command = graph)
b = T.Button(main,text = 'Zakończ', command = end)





l.pack()
e.pack()
b1.pack()

b2.pack()
b.pack()
main.mainloop()
4

1 回答 1

0

GB:x1、x2 只有空字符串。Entry当有一些值时,您必须从graph()函数中获取值Entry- 并记住将其从字符串转换为整数。

PL: x1, x2 zawierają tylko puste stringi。Musisz pobrać wartości z Entryw funkcji graph()kiedy Entryjuż zawierają jakieś wartości - i pamiętaj przekonwertować je z napisów na liczby

工作代码 / działający kod

# -*- coding: utf-8 -*-

import matplotlib.pyplot as mp
import Tkinter as T, sys

class Application():

    def __init__(self, root):

        self.root = root

        self.x1 = []
        self.y2 = []

        self.z = 0

        self.root.title('GRAPH')
        self.root.geometry('600x600')
        self.root.config(bg="#3366ff")

        self.e = T.Entry(self.root, justify='center')

        self.l = T.Label(self.root, text='Podaj liczbę parametrów N =', bg='#3366ff')

        self.b1 = T.Button(self.root, text='OK', command=self.check)

        self.b2 = T.Button(self.root, text='Rysuj', command=self.graph)
        self.b = T.Button(self.root, text='Zakończ', command=self.end)

        self.l.pack()
        self.e.pack()
        self.b1.pack()

        self.b2.pack()
        self.b.pack()

    #------------------------------------------------------------

    def run(self):
        self.root.mainloop()

    #------------------------------------------------------------

    def end(self):
        sys.exit()

    #------------------------------------------------------------

    def check(self):
        try:
            self.entry_x = []
            self.entry_y = []

            self.z = int(self.e.get())

            self.e.config(bg='green')
            self.e.after(1000, lambda: self.e.config(bg='white'))

            self.l2 = T.Label(self.root, text='X', bg='yellow')
            self.l2.pack()

            for i in range(self.z):
                self.entry_x.append(T.Entry(self.root, justify='center'))
                self.entry_x[-1].pack()

            self.l3 = T.Label(self.root, text='Y', bg='#3366ff')
            self.l3.pack()

            for i in range(self.z):
                self.entry_y.append(T.Entry(self.root, justify='center'))
                self.entry_y[-1].pack() 
        except:
            self.e.config(bg='red')
            self.e.after(1000, lambda: self.e.config(bg='white'))

    #------------------------------------------------------------

    def graph(self):

        self.x1 = []
        self.y1 = []

        for i in range(len(self.entry_x)):
            self.x1.append(float(self.entry_x[i].get()))

        for i in range(len(self.entry_y)):
            self.y1.append(float(self.entry_y[i].get()))

        mp.ion()
        mp.plot(self.x1, self.y1)  
        mp.title('Wykres')
        mp.xlabel('x')
        mp.ylabel('y')
        mp.draw()

#====================================================================#

Application(T.Tk()).run()
于 2013-11-10T11:56:18.500 回答