Tkinter印刷线方向相反

时间:2019-06-28 12:28:16

标签: python python-3.x tkinter tkinter-canvas

所以我的程序在画布上打印出一条图像,并有一行通过。运行该程序时,中间印有一个正方形,其中有一个圆圈。但是,也意味着在整个画布上有一条对角线。除了行错了方向,我已经使一切正常,应该反过来说,应该在另一侧对角地行,我似乎不知道为什么。我的代码似乎正确,并且如果有任何错误,我认为这与转换有关。提前非常感谢。

""" Enter your name(s) here


"""
from tkinter import *

class Mapping_for_Tkinter:

def __init__(self, xmin, xmax, ymin, ymax, width):
    self.__xmin = xmin
    self.__xmax = xmax
    self.__ymin = ymin
    self.__ymax = ymax
    self.__width = width
    self.__height = width * ((ymax - ymin) / (xmax - xmin))

def set_xmin(self, xmin):
    self.__xmin = xmin
    self.__set_height()  # update height

def set_xmax(self, xmax):
    self.__xmax = xmax
    self.__set_height()

def set_ymin(self, ymin):
    self.__ymin = ymin
    self.__set_height()

def set_ymax(self, ymax):
    self.__ymax = ymax
    self.__set_height()

def set_width(self, width):
    self.__width = width
    self.__set_height()

def __set_height(self):  # when changing size, always call this private method
    self.__height = self.__width * ((self.__ymax - self.__ymin) / (self.__xmax - self.__xmin))

def get_xmin(self):
    return self.__xmin

def get_xmax(self):
    return self.__xmax

def get_ymin(self):
    return self.__ymin

def get_ymax(self):
    return self.__ymax

def get_width(self):
    return self.__width

def get_height(self):
    return self.__height

# TODO: FIX SCALE CONVERSION
########################
########################
########################
def get_x(self, i):
    # retval = ((float(X - A) / (B - A)) * (D - C)) + C
    # a-b is 0 to width-1
    # c-d is xmin to xmax
    return ((float(i) / (self.__width - 1)) * (self.__xmax - self.__xmin)) + self.__xmin

def get_y(self, y):
    return ((float(y) / (self.__height - 1)) * (self.__ymax - self.__ymin)) + self.__ymin

def get_i(self, x):
    # retval = ((float(X - A) / (B - A)) * (D - C)) + C
    # a-b is xmin to xmax
    # c-d is 0 to width-1
    return ((float(x - self.__xmin) / (self.__xmax - self.__xmin)) * (self.__width - 1))

def get_j(self, y):
    return ((float(y - self.__ymin) / (self.__ymax - self.__ymin)) * (self.__width - 1))

def __str__(self):
    return (str(self.__xmin) + " " + str(self.__xmax) + " " + str(self.__ymin) + " " +
            str(self.__ymax) + " " + str(self.__width) + " " + str(self.__height))


""" to complete"""


def main():
m = Mapping_for_Tkinter(-5.0, 5.0, -5.0, 5.0, 500)  # instantiate mapping
print(m)  # print info about object

window = Tk()  # instantiate a tkinter window
canvas = Canvas(window, width=m.get_width(), height=m.get_height(), bg="white")  # create a canvas width*height
canvas.pack()
# create rectangle the Tkinter way
print("Draw rectangle using tkinter coordinates at (100,400) (400,100)")
canvas.create_rectangle(100, 400, 400, 100, outline="black")

# create circle using the mapping
print("Draw circle using math coordinates at center (0,0) with radius 3")
canvas.create_oval(m.get_i(-3.0), m.get_j(-3.0), m.get_i(3.0), m.get_j(3.0), outline="blue")

# create y=x line pixel by pixel using the mapping
print("Draw line math equation y=x pixel by pixel using the mapping")
for i in range(m.get_width()):
    x = m.get_x(i)  # obtain the x coordinate
    y = x
    canvas.create_rectangle((m.get_i(x), m.get_j(y)) * 2, outline="green")

window.mainloop()  # wait until the window is closed


if __name__ == "__main__":
main()

0 个答案:

没有答案