Hello. Beginner here so bear with me. I'm creating a game as a final project for my university but at the moment I'm stuck. I'm just wondering how you go about making buttons. In the code, I have created four rectangles which will eventually be able to be clicked on. I'm just wondering if there is a way to make a button out of the rectangles that are there. Also, is there any way to write text into the middle of the rectangles rather than doing it manually with code such as Text(point(x,y))... Any suggestions on that or what I have so far would be appreciated. Thanks everyone.

from graphics import *

win = GraphWin("Your Choices", 800,800)

win.setBackground("Red4")
RecAdd = Rectangle(Point(100,50), Point(700,150))
RecAdd.setFill("Aquamarine")
RecAdd.draw(win)


RecAdd = Rectangle(Point(100,200), Point(700,300))
RecAdd.setFill("Aquamarine")
RecAdd.draw(win)

RecAdd = Rectangle(Point(100,350), Point(700,450))
RecAdd.setFill("Aquamarine")
RecAdd.draw(win)

RecAdd = Rectangle(Point(100,500), Point(700,600))
RecAdd.setFill("Aquamarine")
RecAdd.draw(win)

Text(Point(400,100), "Addition").draw(win)

Text(Point(400,250), "Subtraction").draw(win)

Text(Point(400,400), "Division").draw(win)

Text(Point(400,550), "Multiplication").draw(win)

I use wxpython, but it's all about preference.

from wxPython.wx import *

class MyFrame(wxFrame):
	def __init__(self, parent, ID, title):
		wxFrame.__init__(self, parent, ID, title,
								wxDefaultPosition, wxSize(1000, 500))


class MyApp(wxApp):
	def OnInit(self):
		frame = MyFrame(NULL, -1, "Blank GUI")
		frame.Show(true)
		self.SetTopWindow(frame)
		return true

app = MyApp(0)
app.MainLoop()

that'll give you a nice default shell and you should be able to keep re-use any code that you've already written by importing your existing classes into your GUI. It's a good idea to keep functional code away from graphical code in my opinion, as it gives that little bit of flexibility.

see http://www.wxpython.org/tutorial.php for a nice guide that i also used to get started.

LarZ

You should look at Tkinter (available in the normal python distrib) - It would be much simpler than reinvent the wheel.
http://www.ferg.org/thinking_in_tkinter/index.html

Thanks for the nice Tkinter GUI kit reference.

Actually graphics.py was written by John Zelle for use with the book "Python Programming: An Introduction to Computer Science" (Franklin, Beedle & Associates) as a wrapper for Tkinter and itself imports Tkinter. You should be able to use the Button class from Tkinter directly. Try something like graphics.Tkinter.Button(), but then that wouldn't be in the spirit of the assignment.

This question has already been answered. Start a new discussion instead.