SoFunction
Updated on 2024-10-29

Example of python interactive graphics programming (I)

This article example shares specific code for python interactive graphics programming for your reference, the details are as follows

#!/usr/bin/env python3# -*- coding: utf-8 -*-
#Temperature conversion

from graphics import *
 
win = GraphWin("Celsius temperature converter.", 400, 300)
(0.0, 0.0, 3.0, 4.0)
# Drawing interfaces
Text(Point(1,3), " Celsius temperature:").draw(win)
Text(Point(1,1), " Fahrenheit:").draw(win)
input = Entry(Point(2,3), 5)
("0.0")
(win)
output = Text(Point(2,1),"")
(win)
button = Text(Point(1.5,2.0),"Conversion.")
(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# Waiting for the mouse to click
()
# Conversion inputs
celsius = eval(())
fahrenheit = 9.0/5.0 * celsius + 32.0
# Display output, change buttons
(fahrenheit)
("Exit.")
# Wait for a response to a mouse click and exit the program
()
()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Square move

from tkinter import *
 
def main():  
  tk = Tk()
  canvas = Canvas(tk, width = 400, height = 400)
  ()
 
  def moverectangle(event):
    if  == "Up":
      (1,0,-5)
    elif  == "Down":
      (1,0,5)
    elif  == "Left":
      (1,-5,0)
    elif  == "Right":
      (1,5,0)
     
  canvas.create_rectangle(180,180,220,220,fill="red")
  canvas.bind_all("<KeyPress-Up>",moverectangle)
  canvas.bind_all("<KeyPress-Down>",moverectangle)
  canvas.bind_all("<KeyPress-Left>",moverectangle)
  canvas.bind_all("<KeyPress-Right>",moverectangle)
  ()
 
if __name__ == '__main__':
  main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from graphics import *

 
def convert(input):
  celsius = eval(())  # Input conversion
  fahrenheit = 9.0/5.0 * celsius + 32
  return fahrenheit 
def colorChange(win,input):
  cnum = eval(())
  weight = cnum / 100.0
  newcolor = color_rgb(int(255*weight),int(66+150*(1-weight)),int(255*(1-weight)))
  (newcolor)
def main():
  win = GraphWin("Celsius temperature conversion", 400, 300)
  (0.0, 0.0, 3.0, 4.0)
  # Drawing input interfaces
  Text(Point(1,3),
     " Celsius temperature:").draw(win)
  Text(Point(2,2.7),
     " (Please enter: 0.0-100.0 )").draw(win)
  Text(Point(1,1),
     "Fahrenheit:").draw(win)
  input = Entry(Point(2,3), 5)
  ("0.0")
  (win)
  output = Text(Point(2,1),"")
  (win)
  button = Text(Point(1.5,2.0),"Conversion.")
  (win)
  rect = Rectangle(Point(1,1.5), Point(2,2.5))
  (win)
  # Waiting for the mouse to click
  ()
  result = convert(input)  # Conversion inputs
  (result)  # Display output
  # Change the color
  colorChange(win,input)
  # Change the button font
  ("Exit.")
  # Wait for the click event to exit the program
  ()
  ()
 
if __name__ == '__main__':
  main()

This is the whole content of this article.