Created
November 3, 2017 16:26
-
-
Save nikhilkumarsingh/85501ee2c3d8c0cfa9d1a27be5781f06 to your computer and use it in GitHub Desktop.
A simple paint application using tkinter in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
from tkinter.colorchooser import askcolor | |
class Paint(object): | |
DEFAULT_PEN_SIZE = 5.0 | |
DEFAULT_COLOR = 'black' | |
def __init__(self): | |
self.root = Tk() | |
self.pen_button = Button(self.root, text='pen', command=self.use_pen) | |
self.pen_button.grid(row=0, column=0) | |
self.brush_button = Button(self.root, text='brush', command=self.use_brush) | |
self.brush_button.grid(row=0, column=1) | |
self.color_button = Button(self.root, text='color', command=self.choose_color) | |
self.color_button.grid(row=0, column=2) | |
self.eraser_button = Button(self.root, text='eraser', command=self.use_eraser) | |
self.eraser_button.grid(row=0, column=3) | |
self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL) | |
self.choose_size_button.grid(row=0, column=4) | |
self.c = Canvas(self.root, bg='white', width=600, height=600) | |
self.c.grid(row=1, columnspan=5) | |
self.setup() | |
self.root.mainloop() | |
def setup(self): | |
self.old_x = None | |
self.old_y = None | |
self.line_width = self.choose_size_button.get() | |
self.color = self.DEFAULT_COLOR | |
self.eraser_on = False | |
self.active_button = self.pen_button | |
self.c.bind('<B1-Motion>', self.paint) | |
self.c.bind('<ButtonRelease-1>', self.reset) | |
def use_pen(self): | |
self.activate_button(self.pen_button) | |
def use_brush(self): | |
self.activate_button(self.brush_button) | |
def choose_color(self): | |
self.eraser_on = False | |
self.color = askcolor(color=self.color)[1] | |
def use_eraser(self): | |
self.activate_button(self.eraser_button, eraser_mode=True) | |
def activate_button(self, some_button, eraser_mode=False): | |
self.active_button.config(relief=RAISED) | |
some_button.config(relief=SUNKEN) | |
self.active_button = some_button | |
self.eraser_on = eraser_mode | |
def paint(self, event): | |
self.line_width = self.choose_size_button.get() | |
paint_color = 'white' if self.eraser_on else self.color | |
if self.old_x and self.old_y: | |
self.c.create_line(self.old_x, self.old_y, event.x, event.y, | |
width=self.line_width, fill=paint_color, | |
capstyle=ROUND, smooth=TRUE, splinesteps=36) | |
self.old_x = event.x | |
self.old_y = event.y | |
def reset(self, event): | |
self.old_x, self.old_y = None, None | |
if __name__ == '__main__': | |
Paint() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can someone review my code . I am not getting final expected output - import tkinter as tk
import zipfile
import os
create a directory to store the extracted images
if not os.path.exists('images'):
os.mkdir('images')
extract the contents of the images.zip file to the images directory
with zipfile.ZipFile('images.zip', 'r') as zip_ref:
zip_ref.extractall('images')
#Step 9: Implement the "Build Pizza" Button
class PizzaBuilder(Frame):
def init(self, parent):
super().init(parent)
#build_button = Button(self, text="Build Pizza", command=self.build_pizza_handler)
#build_button.grid(row=9, column=0, padx=5, pady=5)
def build_pizza_handler(self):
# Collect user's selected options
pizza_size = self.pizza_size.get()
crust = self.crust.get()
sauce = self.sauce.get()
cheeses = self.cheeses.curselection()
toppings = [topping.get() for topping in self.toppings]
seasoning = self.seasoning_var.get()
############################################################################################
def update_pizza_image(self):
# Collect user's selected options
pizza_size = self.size_var.get()
crust = self.crust_var.get()
sauce = self.sauce_var.get()
############################################################################################
#Step 10: Initialize the GUI
class PizzaBuilder(tk.Frame):
def init(self, parent):
super().init(parent)
if name == 'main':
root = tk.Tk()
root.title("Procedural Object-Oriented Pizza Company")
root.geometry("800x600")
################################################################################################
import os
import uuid
import base64
from PIL import Image
from io import BytesIO
from typing import Dict, List
from fastapi import FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
app = FastAPI()
Allow Cross-Origin Resource Sharing (CORS)
app.add_middleware(
CORSMiddleware,
allow_origins=[""],
allow_credentials=True,
allow_methods=[""],
allow_headers=["*"],
)
class Pizza(BaseModel):
crust: str
cheese: List[str]
sauce: str
toppings: List[str]
def generate_pizza_image(pizza: Pizza) -> str:
# Load the pizza image
pizza_image_path = os.path.join("images", "pizza.jpg")
pizza_image = Image.open(pizza_image_path)
@app.post("/build_pizza")
async def build_pizza_handler(pizza: Pizza):
try:
# Generate the pizza image
pizza_image = generate_pizza_image(pizza)