import tkinter as tk
from tkinter import messagebox
# Initialize the main window
root = tk.Tk()
root.title("Tic-Tac-Toe")
root.configure(bg='lightblue') # Set background color
# Variables to store the state of the game
board = [' ' for _ in range(9)]
player = 'X'
scores = {'X': 0, 'O': 0}
# Function to update the button text and board state
def update_board(index, button):
global player
if board[index] == ' ':
board[index] = player
button.config(text=player, fg='blue' if player == 'X' else 'green', bg='white')
if check_win():
messagebox.showinfo("Tic-Tac-Toe", f"Player {player} wins!")
scores[player] += 1
reset_board()
elif ' ' not in board:
messagebox.showinfo("Tic-Tac-Toe", "No winner!")
reset_board()
else:
player = 'O' if player == 'X' else 'X'
# Function to check for a win
def check_win():
win_conditions = [
(0, 1, 2), (3, 4, 5), (6, 7, 8), # Rows
(0, 3, 6), (1, 4, 7), (2, 5, 8), # Columns
(0, 4, 8), (2, 4, 6) # Diagonals
]
for i, j, k in win_conditions:
if board[i] == board[j] == board[k] != ' ':
return True
return False
# Function to reset the board
def reset_board():
global board, player
board = [' ' for _ in range(9)]
player = 'X'
for button in buttons:
button.config(text='', bg='lightgrey')
# Function to show the scores when closing the game
def on_closing():
messagebox.showinfo("Scores", f"Player X: {scores['X']} wins\nPlayer O: {scores['O']} wins")
root.destroy()
# Create buttons for the Tic-Tac-Toe board
buttons = []
for i in range(9):
button = tk.Button(root, text=' ', width=10, height=5, bg='lightgrey',
command=lambda i=i: update_board(i, buttons[i]))
button.grid(row=i//3, column=i%3, padx=5, pady=5) # Add padding for better spacing
buttons.append(button)
# Bind the closing event to the on_closing function
root.protocol("WM_DELETE_WINDOW", on_closing)
# Run the main loop
root.mainloop()
#tikinter | #game | آموزش پایتون
1.6M حجم رسانه بالاست
مشاهده در ایتا
Morse Code Converter
🧑💻تبدیل کنندهٔ کد مورس
#tikinter | #morse_code
Morse Code Converter
🔺تبدیل کننده کد مورس
import tkinter as tk
from tkinter import messagebox
MORSE_CODE_DICT_ENGLISH = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.',
'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.',
'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-',
'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', '1': '.----',
'2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.', '0': '-----', ' ': '/'
}
MORSE_CODE_DICT_PERSIAN = {
'ا': '.-', 'ب': '-...', 'پ': '.--.', 'ت': '-', 'ث': '...-', 'ج': '.---', 'چ': '---.',
'ح': '....', 'خ': '----', 'د': '-..', 'ذ': '-...', 'ر': '.-.', 'ز': '--..', 'ژ': '--.-',
'س': '...', 'ش': '---', 'ص': '-.-.', 'ض': '-..-', 'ط': '-.-', 'ظ': '-.-.', 'ع': '--',
'غ': '-.-', 'ف': '..-.', 'ق': '--.-', 'ک': '-.-', 'گ': '--.', 'ل': '.-..', 'م': '--',
'ن': '-.', 'و': '.--', 'ه': '....', 'ی': '..', 'ء': '.', 'آ': '.-', 'ژ': '.-.-',
'چ': '--..', ' ': '/'
}
def text_to_morse(text, language):
if language == "English":
morse_dict = MORSE_CODE_DICT_ENGLISH
elif language == "Persian":
morse_dict = MORSE_CODE_DICT_PERSIAN
morse_code = ''
for char in text.upper():
morse_code += morse_dict.get(char, '?') + ' '
return morse_code.strip()
def morse_to_text(morse_code, language):
if language == "English":
morse_dict = {v: k for k, v in MORSE_CODE_DICT_ENGLISH.items()}
elif language == "Persian":
morse_dict = {v: k for k, v in MORSE_CODE_DICT_PERSIAN.items()}
words = morse_code.split(' ')
translated_text = ''
for word in words:
letters = word.split(' ')
for letter in letters:
translated_text += morse_dict.get(letter, '?')
translated_text += ' '
return translated_text.strip()
def convert():
input_text = text_entry.get()
conversion_type = conversion_var.get()
if conversion_type == "English to Morse":
result = text_to_morse(input_text, "English")
elif conversion_type == "Persian to Morse":
result = text_to_morse(input_text, "Persian")
elif conversion_type == "Morse to English":
result = morse_to_text(input_text, "English")
elif conversion_type == "Morse to Persian":
result = morse_to_text(input_text, "Persian")
result_var.set(result)
def copy_to_clipboard():
root.clipboard_clear()
root.clipboard_append(result_var.get())
messagebox.showinfo("Copied", "The result has been copied to the clipboard")
# Create the main window
root = tk.Tk()
root.title("Morse Code Converter")
# Set background color
root.configure(bg='#2D4059')
# Create widgets
title_label = tk.Label(root, text="Morse Code Converter", font=("Helvetica", 16, "bold"), bg='#2D4059', fg='#FFD460')
title_label.pack(pady=10)
text_label = tk.Label(root, text="Enter Text or Morse Code:", bg='#2D4059', fg='white')
text_label.pack(pady=5)
text_entry = tk.Entry(root, width=50)
text_entry.pack(pady=5)
conversion_var = tk.StringVar(value="English to Morse")
conversion_menu = tk.OptionMenu(root, conversion_var, "English to Morse", "Persian to Morse", "Morse to English", "Morse to Persian")
conversion_menu.pack(pady=10)
convert_button = tk.Button(root, text="Convert", command=convert, bg='#FF894C', fg='white')
convert_button.pack(pady=10)
copy_button = tk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard, bg='#DC2F2F', fg='white')
copy_button.pack(pady=10)
result_var = tk.StringVar()
result_label = tk.Label(root, textvariable=result_var, bg='#2D4059', fg='red', font=("Helvetica", 14))
result_label.pack(pady=10)
# Run the main loop
root.mainloop()
#tikinter | #morse_code
2M حجم رسانه بالاست
مشاهده در ایتا
Creating a ToDo list in Python
کارهای خود را با این اپلیکیشن ساده و کاربردی به راحتی مدیریت کنید! با چند کلیک وظایف خود را اضافه و حذف کنید و همیشه در مسیر هدفهایتان بمانید! 🎯💡
#tikinter | #todo_list | LearnPython