tkinter to Android app. { Need tkinter to kivy translation }

I am trying to make e tic tac toe game. I wrote the code in tkinter. While I tried to export the code into apk I found out it is not possible. I tried to translate the code into kivy but the code crashes. UI of the expected output of the code is given below.Intro page Rules pagegame screenWinner screen

**Chatgpt,gemini & Blackbox.ai can’t properly translate tkinter to kivy. **

import tkinter as tk
from tkinter import messagebox


class TicTacToe:
    def __init__(self, root):
        self.root = root
        self.root.title("Tic Tac Toe")
        self.root.configure(bg='black')
        self.root.geometry("428x926")
        self.board = [[None, None, None], [None, None, None], [None, None, None]]
        self.current_turn = 'X'
        self.turn_count = {'X': 0, 'O': 0}
        self.moves = {'X': [], 'O': []}
        self.buttons = [[None, None, None], [None, None, None], [None, None, None]]
        self.create_first_intro_screen()

    def create_first_intro_screen(self):
        self.clear_screen()
        intro_frame = tk.Frame(self.root, bg='black')
        intro_frame.pack(expand=True, fill=tk.BOTH)

        game_name_label = tk.Label(intro_frame, text="Tic Tac Toe", font=('Arial', 40), fg='white', bg='black')
        game_name_label.place(x=36, y=253, width=356)

        proceed_button = tk.Button(intro_frame, text="Let's Play", font=('Arial', 20), fg='black', bg='lightgrey',
                                   borderwidth=2, command=self.create_second_intro_screen)
        proceed_button.place(x=36, y=503, width=356)

        footer_label = tk.Label(intro_frame,
                                text="This is an experimental application made by Saha Aditya Kushal Orion.nAll the rules are same as normal games except this game can be too long to play.",
                                font=('Arial', 7), fg='white', bg='black', justify=tk.CENTER)
        footer_label.place(x=36, y=821, width=356)

    def create_second_intro_screen(self):
        self.clear_screen()
        intro_frame = tk.Frame(self.root, bg='black')
        intro_frame.pack(expand=True, fill=tk.BOTH)

        game_name_label = tk.Label(intro_frame, text="Tic Tac Toe", font=('Arial', 40), fg='white', bg='black',
                                   anchor='w')
        game_name_label.place(x=36, y=110, width=356)

        rules_title_label = tk.Label(intro_frame, text="Game rules are as follows*", font=('Arial', 13), fg='white',
                                     bg='black', anchor='w')
        rules_title_label.place(x=36, y=171, width=356)

        rules_text = (
            "1. The game is played on a 3x3 grid.n"
            "2. Players take turns putting their marks (X or O) in empty squares.n"
            "3. The first player to get 3 of their marks in a row (up, down, across,n or diagonally) is the winner.n"
            "4. When all 9 squares are full, the game is over.n"
            "5. Each player can only place their mark 4 times.n"
            "6. If the fourth move does not result in a win, the first move of that player nwill be removed."
        )

        rules_label = tk.Label(intro_frame, text=rules_text, font=('Arial', 8), fg='white', bg='black', justify=tk.LEFT)
        rules_label.place(x=36, y=234)

        proceed_button = tk.Button(intro_frame, text="Let's BEGIN", font=('Arial', 20), fg='black', bg='lightgrey',
                                   borderwidth=2, command=self.create_buttons)
        proceed_button.place(x=36, y=543, width=356)

        footer_label = tk.Label(intro_frame,
                                text="This is an experimental application made by Saha Aditya Kushal Orion.nAll the rules are same as normal games except this game can be too long to play.",
                                font=('Arial', 7), fg='white', bg='black', justify=tk.CENTER)
        footer_label.place(x=36, y=821, width=356)

    def create_buttons(self):
        self.clear_screen()
        self.root.configure(bg='black')

        # Calculate button sizes and grid positions
        grid_width = 356
        grid_height = 356
        button_size = grid_width // 3

        for i in range(3):
            for j in range(3):
                button = tk.Button(self.root, text="", font=('Arial', 40), width=button_size // 10, height=2,
                                   fg='white', bg='black', borderwidth=2, highlightbackground='white',
                                   command=lambda row=i, col=j: self.on_button_click(row, col))
                button.place(x=36 + j * button_size, y=200 + i * button_size, width=button_size, height=button_size)
                self.buttons[i][j] = button

        self.display_turn_label()

    def display_turn_label(self):
        turn_label = tk.Label(self.root, text=f"Turn: {self.current_turn}", font=('Arial', 20), fg='white', bg='black')
        turn_label.place(x=160, y=570)

    def on_button_click(self, row, col):
        if self.buttons[row][col].cget('text') == "" and self.turn_count[self.current_turn] < 4:
            color = 'red' if self.current_turn == 'X' else 'blue'
            self.buttons[row][col].config(text=self.current_turn, fg=color)
            self.board[row][col] = self.current_turn
            self.moves[self.current_turn].append((row, col))
            self.turn_count[self.current_turn] += 1

            if self.check_winner():
                self.show_winner(self.current_turn)
                return

            if self.turn_count[self.current_turn] == 4:
                self.handle_fourth_move()

            self.current_turn = 'O' if self.current_turn == 'X' else 'X'
            self.update_turn_label()

    def update_turn_label(self):
        turn_label = tk.Label(self.root, text=f"Turn: {self.current_turn}", font=('Arial', 20), fg='white', bg='black')
        turn_label.place(x=160, y=570)

    def handle_fourth_move(self):
        first_move = self.moves[self.current_turn].pop(0)
        row, col = first_move
        self.buttons[row][col].config(text="", fg='white')
        self.board[row][col] = None
        self.turn_count[self.current_turn] -= 1

    def check_winner(self):
        for i in range(3):
            if self.board[i][0] == self.board[i][1] == self.board[i][2] == self.current_turn:
                return True
            if self.board[0][i] == self.board[1][i] == self.board[2][i] == self.current_turn:
                return True
        if self.board[0][0] == self.board[1][1] == self.board[2][2] == self.current_turn:
            return True
        if self.board[0][2] == self.board[1][1] == self.board[2][0] == self.current_turn:
            return True
        return False

    def show_winner(self, winner):
        self.clear_screen()
        result_frame = tk.Frame(self.root, bg='black')
        result_frame.pack(expand=True, fill=tk.BOTH)

        winner_label = tk.Label(result_frame, text=f"{winner} nWON", font=('Arial', 40), fg='white', bg='black',justify=tk.CENTER)
        winner_label.place(x=166, y=312)

        play_again_button = tk.Button(result_frame, text="Play Again", font=('Arial', 20), fg='black', bg='lightgrey',
                                      borderwidth=2, command=self.reset_board)
        play_again_button.place(x=36, y=543, width=356)

        footer_label = tk.Label(result_frame, text="Thank you playing.",
                                font=('Arial', 10), fg='white', bg='black', justify=tk.CENTER)
        footer_label.place(x=36, y=821, width=356)

    def reset_board(self):
        self.board = [[None, None, None], [None, None, None], [None, None, None]]
        self.current_turn = 'X'
        self.turn_count = {'X': 0, 'O': 0}
        self.moves = {'X': [], 'O': []}

        # Clear the screen first
        self.clear_screen()

        # Recreate buttons and display whose turn it is
        self.create_buttons()
        self.display_turn_label()

    def clear_screen(self):
        for widget in self.root.winfo_children():
            widget.destroy()


if __name__ == "__main__":
    root = tk.Tk()
    game = TicTacToe(root)
    root.mainloop()

New contributor

Saha Aditya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật