Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Graphics error
#1
What I want to be able to do is click on the frame window so that points will appear on it and when I click in the rectangle that says 'Done' for the program to stop. However the program keep going when I click inside the rectangle. Here IS the code I have so far:
def main():
    print(" Click anywhere in graph window ten times to get points")
    win=GraphWin("regression line", 300,300)
    rect= Rectangle(Point(5,5), Point(90,95))
    rect.draw(win)
    label= Text( Point(30,50), "Done")
    label.draw(win)
    
    while True:
        click_point=win.getMouse()
        mouse= click_point
        click_x=mouse.getX()
        click_y=mouse.getY()    
        p1=rect.getP1()
        p2=rect.getP2()
        rect_x1, rect_y1= p1.getX(), p1.getY()
        rect_x2, rect_y2= p2.getX(), p1.getY()
        mouse.draw(win)
        
        if (rect_x1<=click_x<=rect_x2) and (rect_y1<=click_y<=rect_y2): break
Larz60+ write Dec-06-2025, 06:37 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button. I have aded bbcode tags for youthis time.
Reply
#2
(Dec-05-2025, 09:32 PM)nathanael Wrote: win=GraphWin("regression line", 300,300)
Shouild not GraphWin it was module(wrapper around Tkinter) written by John Zelle for his book in 2005.
Just use Tkinter not a module that not been updatet in 20 year.
Pedroski55 likes this post
Reply
#3
        if (rect_x1<=click_x<=rect_x2) and (rect_y1<=click_y<=rect_y2): break
To debug this, print the variables to see if both sides of the "and" are true. To reiterate the other post, Graphics is no longer maintained because there are better options: Tkinter and Turtle --> comes with Python and is also a subset of Tkinter.
## a "click the rectangle" in tkinter
## to show that it is not difficult to learn
import tkinter as tk

root=tk.Tk()
root.geometry("+100+100")

def bye_bye(event=None):
    print("Bye, Bye")
    root.quit()

canvas = tk.Canvas(root)
tk_id = canvas.create_rectangle((10, 10, 100, 150),
                                 fill="red", tags="rec")
canvas.tag_bind("rec", "<Button-1>", bye_bye)
canvas.grid()

root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python graphics kaltenherz 1 2,893 Sep-05-2021, 05:19 PM
Last Post: jefsummers
  from graphics import * jmaloney413 2 8,631 Oct-18-2018, 08:22 AM
Last Post: perfringo
  Graphics py not found vnc 13 33,304 Apr-05-2017, 05:36 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020