Generator wykresów

Stronę tą wyświetlono już: 432 razy

Program wykres generuje wykres rysowany na podstawie danych podanych przez użytkownika. Program został napisany w Pythonie 3+ z wykorzystaniem biblioteki tkinter. Oto kod programu:

Listing 1
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import tkinter as tk
  4. from tkinter import filedialog as fd
  5. from math import *
  6. x = 0
  7. def main():
  8. class AppUI(tk.Frame):
  9. def __init__(self, master=None):
  10. tk.Frame.__init__(self, master, relief=tk.SUNKEN, bd=2)
  11. self.menubar = tk.Menu(self)
  12. menu = tk.Menu(self.menubar, tearoff=0)
  13. self.menubar.add_cascade(label="Program", menu=menu)
  14. menu.add_command(label="Wyjście", command = self.quit)
  15. menu = tk.Menu(self.menubar, tearoff=0)
  16. self.menubar.add_cascade(label="Wykres", menu=menu)
  17. menu.add_command(label="Zapisz jako *.ps", command = self.saveAsPs)
  18. self.l_equation = tk.Label(window, text = "Podaj funkcję, której wykres ma być narysowany:")
  19. self.l_equation.pack(anchor = tk.NW)
  20. self.e_equation = tk.Entry(window)
  21. self.e_equation.insert(0,"sin(x)")
  22. self.e_equation.pack(anchor = tk.NW)
  23. self.l_xmin = tk.Label(window, text = "x minimalne:")
  24. self.l_xmin.pack(anchor = tk.NW)
  25. self.e_xmin = tk.Entry(window)
  26. self.e_xmin.insert(0, "-pi")
  27. self.e_xmin.pack(anchor = tk.NW)
  28. self.l_xmax = tk.Label(window, text = "x maksymalne:")
  29. self.l_xmax.pack(anchor = tk.NW)
  30. self.e_xmax = tk.Entry(window)
  31. self.e_xmax.insert(0, "pi")
  32. self.e_xmax.pack(anchor = tk.NW)
  33. self.b_draw = tk.Button(window, text="Rysuj wykres", command = self.draw)
  34. self.b_draw.pack(anchor = tk.NW)
  35. self.c_draw = tk.Canvas(window, width = 500, height = 300)
  36. self.c_draw.pack(anchor = tk.NW, expand = tk.YES, fill = tk.BOTH
  37. )
  38. try:
  39. self.master.config(menu=self.menubar)
  40. except AttributeError:
  41. # master is a toplevel window (Python 1.4/Tkinter 1.63)
  42. self.master.tk.call(master, "config", "-menu", self.menubar)
  43. def saveAsPs(self):
  44. filename = fd.asksaveasfilename(parent = self.master, filetypes = [('postscript', '.ps'), ('all files', '.*')], initialfile = "wykres.ps")
  45. if filename:
  46. f = open(filename, "w")
  47. f.write(self.c_draw.postscript())
  48. f.close()
  49. def draw(self):
  50. self.c_draw.delete("all")
  51. global x
  52. xmin = 0.
  53. xmax = pi
  54. height = self.c_draw.winfo_height() - 6
  55. width = self.c_draw.winfo_width() - 2
  56. self.c_draw.create_rectangle([0,0,width + 2,height + 2], fill="white")
  57. try:
  58. xmin = eval(self.e_xmin.get())
  59. except:
  60. xmin = 0
  61. try:
  62. xmax = eval(self.e_xmax.get())
  63. except:
  64. xmax = pi
  65. if xmax < xmin:
  66. xmax, xmin = xmin, xmax
  67. elif xmax == xmin:
  68. xmax = xmin + pi
  69. step = (xmax - xmin) / width
  70. l = []
  71. for i in range(width):
  72. x = xmin + i * step
  73. try:
  74. y = eval(self.e_equation.get())
  75. except:
  76. y = 0
  77. l.append((x - xmin) * width / (xmax - xmin) )
  78. l.append(- y)
  79. l2 = l[1::2]
  80. ymin = min(l2)
  81. ymax = max(l2)
  82. if ymin == ymax:
  83. if ymin < 0:
  84. ymax = 0
  85. elif ymax > 0:
  86. ymin = 0
  87. else:
  88. ymin = -1
  89. ymax = 1
  90. l3 = []
  91. for i in zip(l[0::2],l[1::2]):
  92. l3.append(i[0])
  93. l3.append((i[1] - ymin) * height / (ymax - ymin) + 2)
  94. self.c_draw.create_line([0, - ymin * height / (ymax - ymin) + 2, width, - ymin * height / (ymax - ymin) + 2], fill="red", width=1.0)
  95. self.c_draw.create_line([- xmin * width / (xmax - xmin), 0, -xmin * width / (xmax - xmin), height], fill="blue", width=1.0)
  96. self.c_draw.create_line(l3, fill="green", width = 2.)
  97. self.c_draw.create_text([5, -ymin * height / (ymax - ymin) +2],text="0")
  98. self.c_draw.create_text([-xmin * width / (xmax - xmin), height - 10], text="0")
  99. window = tk.Tk()
  100. window.title("Generator wykresów")
  101. app = AppUI(window)
  102. app.pack()
  103. window.mainloop()
  104. return 0
  105. if __name__ == '__main__':
  106. main()

Poniżej zamieszczam screen programu.

Program napisany w Pythonie generujący wykresy funkcji 2w
Rys. 1
Program napisany w Pythonie generujący wykresy funkcji 2W

Komentarze