Tkinter হল একটি আদর্শ পাইথন লাইব্রেরি যা GUI-ভিত্তিক অ্যাপ্লিকেশন তৈরি করতে ব্যবহৃত হয়। একটি সাধারণ চলন্ত বল অ্যাপ্লিকেশন তৈরি করতে, আমরা ক্যানভাস উইজেট ব্যবহার করতে পারি যা ব্যবহারকারীকে ছবি যোগ করতে, আকৃতি আঁকতে এবং বস্তুর অ্যানিমেটিং করতে দেয়। অ্যাপ্লিকেশনটিতে নিম্নলিখিত উপাদান রয়েছে,
-
উইন্ডোতে ডিম্বাকৃতি বা বল আঁকতে একটি ক্যানভাস উইজেট।
-
বল সরানোর জন্য, আমাদের একটি ফাংশন move_ball() সংজ্ঞায়িত করতে হবে . ফাংশনে, আপনাকে বলের অবস্থান নির্ধারণ করতে হবে যা ক্রমাগত আপডেট হবে যখন বলটি ক্যানভাসের দেয়ালে আঘাত করবে (বাম, ডান, উপরে এবং নীচে)।
-
বলের অবস্থান আপডেট করতে, আমাদের canvas.after(dure, function()) ব্যবহার করতে হবে যা একটি নির্দিষ্ট সময়ের পর বলটিকে তার অবস্থান পরিবর্তন করতে প্রতিফলিত করে।
-
অবশেষে, অ্যাপ্লিকেশনটি চালানোর জন্য কোডটি কার্যকর করুন।
উদাহরণ
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")
# Make the window size fixed
win.resizable(False,False)
# Create a canvas widget
canvas=Canvas(win, width=700, height=350)
canvas.pack()
# Create an oval or ball in the canvas widget
ball=canvas.create_oval(10,10,50,50, fill="green3")
# Move the ball
xspeed=yspeed=3
def move_ball():
global xspeed, yspeed
canvas.move(ball, xspeed, yspeed)
(leftpos, toppos, rightpos, bottompos)=canvas.coords(ball)
if leftpos <=0 or rightpos>=700:
xspeed=-xspeed
if toppos <=0 or bottompos >=350:
yspeed=-yspeed
canvas.after(30,move_ball)
canvas.after(30, move_ball)
win.mainloop() আউটপুট
উপরের কোডটি চালানোর ফলে একটি অ্যাপ্লিকেশন উইন্ডো প্রদর্শিত হবে যার ক্যানভাসে একটি চলমান বল থাকবে।
