This is python3. Please help me out. Write a GUI program that draws a rectangle
ID: 3677947 • Letter: T
Question
This is python3. Please help me out.
Write a GUI program that draws a rectangle and/or an oval, as shown in the figures below. The user selects a figure from a radio button and specifies whether it is filled by selecting a check button. A regular button is used to clear all drawings on the canvas. When the Rectangle radio button is clicked, the program checks if the Filled check button is checked to decide whether or not the rectangle should be filled. It then draws a rectangle with top_left corner at (50,50) and bottom_right corner at (250,250). When the oval radio button is clicked, the program checks if the Filled check button is checked to decide whether or not the oval should be filled. It then draws an oval "bounded by an imaginary rectangle" with corners at (50,100) and (250, 200). Use red fill color for the rectangle and yellow fill color for the oval. When the Clear button is clicked, all drawing on the canvas will be cleared and all buttons will be unchecked.
Use nested frames to achieve the shown layout where all buttons are added to a pane/frame that is added to the main frame.
And here is my code. But it doesnt run well.
from tkinter import *
class Canvas(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("GUIs drawing geometric shapes")
self.grid()
self.canvas = Canvas(self, width = 500, height = 500, bg = "white")
self.canvas.grid(row = 0, column = 0)
frame = Frame(self)
frame.grid(row = 1, column = 0)
rectangle = Button(frame, text = "Rectangle", command = self.displayRectangle)
oval = Button(frame, text = "Oval", command = self.displayOval)
clear = Button(frame, text = "Clear", command = self.clearCanvas)
rectangle.grid(row = 0, column = 0)
oval.grid(row = 0, column = 1)
clear.grid(row = 0, column = 2)
def displayRectangle(self):
self.canvas.create_rectangle(10, 10, 190, 90, tags = "rect")
def desplayOval(self):
self.canvas.create_oval(10, 10, 190, 90, tags = "oval")
def crearCanvas(self):
self.canvas.delete("rect", "oval")
def main():
Canvas().mainloop()
main()
Explanation / Answer
from Tkinter import * master = Tk() shape = IntVar() # ensure you use an instance of IntVar Radiobutton(text='Circle', variable=shape, indicatoron=0, value=1, master=master).pack() Radiobutton(text='Rectangle', variable=shape, indicatoron=0, value=2, master=master).pack() to draw a circle import pythongame, system pythongame.init() screen=pygame.display.set_mode([640,480]) screen.fill([0,0,255]) pythongame.draw.circle(screen,[300,0,0],[100,100],50,0) pythongame.display.flip() while True: for event in pythongame.event.get(): if event.type==pythongame.QUIT: system.exit()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.