skip to Main Content

I am trying to write a program in Python using customtkinter that emulates key presses for one game. I have set it up so that at the start of the program, a .json file with the keys specified by the user in comboboxes as the keys they use in the game is created. My question is, how can I make it so that if the keybinds.json file already exists, the app = customtkinter.CTk will not be created, but instead "w = customtkinter.CTk" will be created and the .json file will be read there? Here is the code:

import tkinter
import customtkinter
import keyboard
from time import sleep
import json
import os

customtkinter.set_appearance_mode("dark")  # Modes: system (default), light, dark
customtkinter.set_default_color_theme("dark-blue")  # Themes: blue (default), dark-blue, green

app = customtkinter.CTk()  # create CTk window like you do with the Tk window
app.geometry("600x480")
app.resizable(False, False)
app.title("Keybinds for ENigma")
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()

x_coordinate = (screen_width/2) - (600/2)
y_coordinate = (screen_height/2) - (440/2)

def submit():
    keybinds = {
        "Jump": JumpKey.get(),
        "Aim Up": AIMUp.get(),
        "Move Up": UPMoveKey.get(),
        "Move Left": LEFTMoveKey.get(),
        "Move Right": RIGHTMoveKey.get(),
        "Move Down": DOWNMoveKey.get(),
        "Light Attack": LIGHTATTACKKey.get(),
        "Heavy Attack": HEAVYATTACKKey.get(),
        "Dodge": DodgeKey.get()
    }
    
    with open("keybinds.json", "w") as file:
        json.dump(keybinds, file)
    app.destroy()
    w=customtkinter.CTk()
    w.geometry("620x580")
    w.title('Enigma v0.1')
    w.resizable(False, False)
    screen_width = w.winfo_screenwidth()
    screen_height = w.winfo_screenheight()

    x_coordinate = (screen_width/2) - (620/2)
    y_coordinate = (screen_height/2) - (580/2)


    with open("./keybinds.json") as f:
        keybindData = json.load(f)

    JUMP = keybindData["Jump"]
    AIMUP = keybindData["Aim Up"]
    UP = keybindData["Move Up"]
    LEFT = keybindData["Move Left"]
    RIGHT = keybindData["Move Right"]
    DOWN = keybindData["Move Down"]
    LightAttack = keybindData["Light Attack"]
    HeavyAttack = keybindData["Heavy Attack"]
    DODGE = keybindData["Dodge"]

    w.geometry("+%d+%d" % (x_coordinate, y_coordinate))
    l1=customtkinter.CTkLabel(master=w, text=f"Welcome to Enigma v0.1!", font=('Century Gothic', 35))
    l1.place(x=320, y=25, anchor=tkinter.CENTER)

    # names
    blasters=customtkinter.CTkLabel(master=w, text="Blasters", font=('Century Gothic', 20))
    blasters.place(x=75, y=100)

    #################
    # blasters
    
    cmb1=customtkinter.CTkLabel(master=w, text="DLightDAir", font=('Century Gothic', 14))
    cmb1.place(x=30, y=147)

    cmb2=customtkinter.CTkLabel(master=w, text="DLightSAir", font=('Century Gothic', 14))
    cmb2.place(x=30, y=177)

    cmb3=customtkinter.CTkLabel(master=w, text="DLight Recovery", font=('Century Gothic', 14))
    cmb3.place(x=2, y=203)
   
    ###################
    # trigger keys
    # blasters
    TriggerKey1 = customtkinter.StringVar(None)
    WeaponKeyCombox1 = customtkinter.CTkComboBox(master=w, width=70, height=20, variable=TriggerKey1, values=['None', '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', 'Down', 'DOWN', 'Up', 'UP', 'Left', 'LEFT', 'Right', 'RIGHT', 'Shift', 'SHIFT', 'Ctrl', 'CTRL', 'Esc', 'ESC', 'Alt', 'ALT', 'Space', 'SPACE', 'Enter', 'ENTER','Spacebar', 'SPACEBAR', 'Tab', 'TAB', 'Home', 'HOME', 'End', 'END', 'Plus', 'PLUS', 'Minus', 'MINUS', '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', 'Up arrow key', 'Down arrow key', 'Left arrow key', 'Right arrow key'])
    WeaponKeyCombox1.set('None')
    WeaponKeyCombox1.place(x=115,y=150)

    TriggerKey2 = customtkinter.StringVar(None)
    WeaponKeyCombox2 = customtkinter.CTkComboBox(master=w, width=70, height=20, variable=TriggerKey2, values=['None', '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', 'Down', 'DOWN', 'Up', 'UP', 'Left', 'LEFT', 'Right', 'RIGHT', 'Shift', 'SHIFT', 'Ctrl', 'CTRL', 'Esc', 'ESC', 'Alt', 'ALT', 'Space', 'SPACE', 'Enter', 'ENTER','Spacebar', 'SPACEBAR', 'Tab', 'TAB', 'Home', 'HOME', 'End', 'END', 'Plus', 'PLUS', 'Minus', 'MINUS', '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', 'Up arrow key', 'Down arrow key', 'Left arrow key', 'Right arrow key'])
    WeaponKeyCombox2.set('None')
    WeaponKeyCombox2.place(x=115,y=180)

    TriggerKey3 = customtkinter.StringVar(None)
    WeaponKeyCombox3 = customtkinter.CTkComboBox(master=w, width=70, height=20, variable=TriggerKey3, values=['None', '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', 'Down', 'DOWN', 'Up', 'UP', 'Left', 'LEFT', 'Right', 'RIGHT', 'Shift', 'SHIFT', 'Ctrl', 'CTRL', 'Esc', 'ESC', 'Alt', 'ALT', 'Space', 'SPACE', 'Enter', 'ENTER','Spacebar', 'SPACEBAR', 'Tab', 'TAB', 'Home', 'HOME', 'End', 'END', 'Plus', 'PLUS', 'Minus', 'MINUS', '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', 'Up arrow key', 'Down arrow key', 'Left arrow key', 'Right arrow key'])
    WeaponKeyCombox3.set('None')
    WeaponKeyCombox3.place(x=115,y=210)

    def DLightDAirBlasters(event):
        if event.name == TriggerKey1.get():
            keyboard.press(DOWN)
            keyboard.press_and_release(LightAttack)
            keyboard.release(DOWN)
            sleep(0.69)
            keyboard.press(DOWN)
            keyboard.press_and_release(DODGE)
            keyboard.press_and_release(JUMP)
            keyboard.press_and_release(LightAttack)
            keyboard.release(DOWN)
    keyboard.on_press(DLightDAirBlasters)

    w.mainloop()
    

AIMUp = customtkinter.StringVar(None)
AIMUp = customtkinter.CTkComboBox(master=app, width=100, corner_radius=10, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left', 'right'])
AIMUp.set('None')
AIMUp.place(x=230,y=80)

UPMoveKey = customtkinter.StringVar(None)
UPMoveKey = customtkinter.CTkComboBox(master=app, width=100, corner_radius=10, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left', 'right'])
UPMoveKey.set('None')
UPMoveKey.place(x=250,y=120)

LEFTMoveKey = customtkinter.StringVar(None)
LEFTMoveKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left', 'right'])
LEFTMoveKey.set('None')
LEFTMoveKey.place(x=250,y=160)

RIGHTMoveKey = customtkinter.StringVar(None)
RIGHTMoveKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left', 'right'])
RIGHTMoveKey.set('None')
RIGHTMoveKey.place(x=250,y=200)

DOWNMoveKey = customtkinter.StringVar(None)
DOWNMoveKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left', 'right'])
DOWNMoveKey.set('None')
DOWNMoveKey.place(x=250,y=240)

LIGHTATTACKKey = customtkinter.StringVar(None)
LIGHTATTACKKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'c', 'x', 'z', 'v', 'b', 'n', 'm'])
LIGHTATTACKKey.set('None')
LIGHTATTACKKey.place(x=250,y=280)

HEAVYATTACKKey = customtkinter.StringVar(None)
HEAVYATTACKKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'c', 'x', 'z', 'v', 'b', 'n', 'm'])
HEAVYATTACKKey.set('None')
HEAVYATTACKKey.place(x=250,y=320)

DodgeKey = customtkinter.StringVar(None)
DodgeKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30, values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'c', 'x', 'z', 'v', 'b', 'n', 'm'])
DodgeKey.set('None')
DodgeKey.place(x=250,y=360)

JumpKey = customtkinter.StringVar(None)
JumpKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30, values=['None', 'up', 'down', 'left', 'right', 'space', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'c', 'x', 'z', 'v', 'b', 'n', 'm'])
JumpKey.set('None')
JumpKey.place(x=250,y=400)

label1 = customtkinter.CTkLabel(master=app, text="Define the keys you use for input in game", font=('Century Gothic', 25))
label1.place(x=310, y=50, anchor=tkinter.CENTER)

labelaimup = customtkinter.CTkLabel(master=app, text="Aim Up: ", font=('Century Gothic', 15))
labelaimup.place(x=160, y=80)

labelaimup2 = customtkinter.CTkLabel(master=app, text="You must bind the Aim Up key in game!", font=('Century Gothic', 13))
labelaimup2.place(x=340, y=80)

labelup = customtkinter.CTkLabel(master=app, text="Move Up: ", font=('Century Gothic', 15))
labelup.place(x=150, y=120)

labelleft = customtkinter.CTkLabel(master=app, text="Move Left: ", font=('Century Gothic', 15))
labelleft.place(x=150, y=160)

labelright = customtkinter.CTkLabel(master=app, text="Move Right: ", font=('Century Gothic', 15))
labelright.place(x=150, y=200)

labeldown = customtkinter.CTkLabel(master=app, text="Move Down: ", font=('Century Gothic', 15))
labeldown.place(x=140, y=240)

labellightattack = customtkinter.CTkLabel(master=app, text="Light Attack: ", font=('Century Gothic', 15))
labellightattack.place(x=150, y=280)

labelheavyattack = customtkinter.CTkLabel(master=app, text="Heavy Attack: ", font=('Century Gothic', 15))
labelheavyattack.place(x=135, y=320)

labeldodge = customtkinter.CTkLabel(master=app, text="Dodge/Dash: ", font=('Century Gothic', 15))
labeldodge.place(x=135, y=360)

labeljump = customtkinter.CTkLabel(master=app, text="Jump: ", font=('Century Gothic', 15))
labeljump.place(x=175, y=400)

button = customtkinter.CTkButton(master=app, text="Submit", corner_radius=10, width=50, command=submit)
button.place(x=260, y=445)

app.geometry("+%d+%d" % (x_coordinate, y_coordinate))

app.mainloop()

i dont know what to try

2

Answers


  1. If statement at the end inserted and loading from there, passing loaded data to submit. However, app still gets created and destroyed. You will have to refactor your code to smaller functions and call them appropriately to avoid this.

    import tkinter
    import customtkinter
    import keyboard
    from time import sleep
    import json
    from os.path import exists
    
    customtkinter.set_appearance_mode("dark")  # Modes: system (default), light, dark
    customtkinter.set_default_color_theme("dark-blue")  # Themes: blue (default), dark-blue, green
    
    app = customtkinter.CTk()  # create CTk window like you do with the Tk window
    app.geometry("600x480")
    app.resizable(False, False)
    app.title("Keybinds for ENigma")
    screen_width = app.winfo_screenwidth()
    screen_height = app.winfo_screenheight()
    
    x_coordinate = (screen_width / 2) - (600 / 2)
    y_coordinate = (screen_height / 2) - (440 / 2)
    
    
    def submit(keys=None):
        if not keys:
            keybinds = {
                "Jump": JumpKey.get(),
                "Aim Up": AIMUp.get(),
                "Move Up": UPMoveKey.get(),
                "Move Left": LEFTMoveKey.get(),
                "Move Right": RIGHTMoveKey.get(),
                "Move Down": DOWNMoveKey.get(),
                "Light Attack": LIGHTATTACKKey.get(),
                "Heavy Attack": HEAVYATTACKKey.get(),
                "Dodge": DodgeKey.get()
            }
        else:
            keybinds = keys
    
        with open("keybinds.json", "w") as file:
            json.dump(keybinds, file)
        app.destroy()
        w = customtkinter.CTk()
        w.geometry("620x580")
        w.title('Enigma v0.1')
        w.resizable(False, False)
        screen_width = w.winfo_screenwidth()
        screen_height = w.winfo_screenheight()
    
        x_coordinate = (screen_width / 2) - (620 / 2)
        y_coordinate = (screen_height / 2) - (580 / 2)
    
        with open("./keybinds.json") as f:
            keybindData = json.load(f)
    
        JUMP = keybindData["Jump"]
        AIMUP = keybindData["Aim Up"]
        UP = keybindData["Move Up"]
        LEFT = keybindData["Move Left"]
        RIGHT = keybindData["Move Right"]
        DOWN = keybindData["Move Down"]
        LightAttack = keybindData["Light Attack"]
        HeavyAttack = keybindData["Heavy Attack"]
        DODGE = keybindData["Dodge"]
    
        w.geometry("+%d+%d" % (x_coordinate, y_coordinate))
        l1 = customtkinter.CTkLabel(master=w, text=f"Welcome to Enigma v0.1!", font=('Century Gothic', 35))
        l1.place(x=320, y=25, anchor=tkinter.CENTER)
    
        # names
        blasters = customtkinter.CTkLabel(master=w, text="Blasters", font=('Century Gothic', 20))
        blasters.place(x=75, y=100)
    
        #################
        # blasters
    
        cmb1 = customtkinter.CTkLabel(master=w, text="DLightDAir", font=('Century Gothic', 14))
        cmb1.place(x=30, y=147)
    
        cmb2 = customtkinter.CTkLabel(master=w, text="DLightSAir", font=('Century Gothic', 14))
        cmb2.place(x=30, y=177)
    
        cmb3 = customtkinter.CTkLabel(master=w, text="DLight Recovery", font=('Century Gothic', 14))
        cmb3.place(x=2, y=203)
    
        ###################
        # trigger keys
        # blasters
        TriggerKey1 = customtkinter.StringVar(None)
        WeaponKeyCombox1 = customtkinter.CTkComboBox(master=w, width=70, height=20, variable=TriggerKey1,
                                                     values=['None', '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', 'Down', 'DOWN', 'Up', 'UP', 'Left', 'LEFT', 'Right',
                                                             'RIGHT', 'Shift', 'SHIFT', 'Ctrl', 'CTRL', 'Esc', 'ESC', 'Alt',
                                                             'ALT', 'Space', 'SPACE', 'Enter', 'ENTER', 'Spacebar',
                                                             'SPACEBAR', 'Tab', 'TAB', 'Home', 'HOME', 'End', 'END', 'Plus',
                                                             'PLUS', 'Minus', 'MINUS', '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', 'Up arrow key',
                                                             'Down arrow key', 'Left arrow key', 'Right arrow key'])
        WeaponKeyCombox1.set('None')
        WeaponKeyCombox1.place(x=115, y=150)
    
        TriggerKey2 = customtkinter.StringVar(None)
        WeaponKeyCombox2 = customtkinter.CTkComboBox(master=w, width=70, height=20, variable=TriggerKey2,
                                                     values=['None', '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', 'Down', 'DOWN', 'Up', 'UP', 'Left', 'LEFT', 'Right',
                                                             'RIGHT', 'Shift', 'SHIFT', 'Ctrl', 'CTRL', 'Esc', 'ESC', 'Alt',
                                                             'ALT', 'Space', 'SPACE', 'Enter', 'ENTER', 'Spacebar',
                                                             'SPACEBAR', 'Tab', 'TAB', 'Home', 'HOME', 'End', 'END', 'Plus',
                                                             'PLUS', 'Minus', 'MINUS', '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', 'Up arrow key',
                                                             'Down arrow key', 'Left arrow key', 'Right arrow key'])
        WeaponKeyCombox2.set('None')
        WeaponKeyCombox2.place(x=115, y=180)
    
        TriggerKey3 = customtkinter.StringVar(None)
        WeaponKeyCombox3 = customtkinter.CTkComboBox(master=w, width=70, height=20, variable=TriggerKey3,
                                                     values=['None', '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', 'Down', 'DOWN', 'Up', 'UP', 'Left', 'LEFT', 'Right',
                                                             'RIGHT', 'Shift', 'SHIFT', 'Ctrl', 'CTRL', 'Esc', 'ESC', 'Alt',
                                                             'ALT', 'Space', 'SPACE', 'Enter', 'ENTER', 'Spacebar',
                                                             'SPACEBAR', 'Tab', 'TAB', 'Home', 'HOME', 'End', 'END', 'Plus',
                                                             'PLUS', 'Minus', 'MINUS', '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', 'Up arrow key',
                                                             'Down arrow key', 'Left arrow key', 'Right arrow key'])
        WeaponKeyCombox3.set('None')
        WeaponKeyCombox3.place(x=115, y=210)
    
        def DLightDAirBlasters(event):
            if event.name == TriggerKey1.get():
                keyboard.press(DOWN)
                keyboard.press_and_release(LightAttack)
                keyboard.release(DOWN)
                sleep(0.69)
                keyboard.press(DOWN)
                keyboard.press_and_release(DODGE)
                keyboard.press_and_release(JUMP)
                keyboard.press_and_release(LightAttack)
                keyboard.release(DOWN)
    
        #keyboard.on_press(DLightDAirBlasters)
    
        w.mainloop()
    
    
    AIMUp = customtkinter.StringVar(None)
    AIMUp = customtkinter.CTkComboBox(master=app, width=100, corner_radius=10, height=30,
                                      values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left',
                                              'right'])
    AIMUp.set('None')
    AIMUp.place(x=230, y=80)
    
    UPMoveKey = customtkinter.StringVar(None)
    UPMoveKey = customtkinter.CTkComboBox(master=app, width=100, corner_radius=10, height=30,
                                          values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left',
                                                  'right'])
    UPMoveKey.set('None')
    UPMoveKey.place(x=250, y=120)
    
    LEFTMoveKey = customtkinter.StringVar(None)
    LEFTMoveKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30,
                                            values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left',
                                                    'right'])
    LEFTMoveKey.set('None')
    LEFTMoveKey.place(x=250, y=160)
    
    RIGHTMoveKey = customtkinter.StringVar(None)
    RIGHTMoveKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30,
                                             values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left',
                                                     'right'])
    RIGHTMoveKey.set('None')
    RIGHTMoveKey.place(x=250, y=200)
    
    DOWNMoveKey = customtkinter.StringVar(None)
    DOWNMoveKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30,
                                            values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'up', 'down', 'left',
                                                    'right'])
    DOWNMoveKey.set('None')
    DOWNMoveKey.place(x=250, y=240)
    
    LIGHTATTACKKey = customtkinter.StringVar(None)
    LIGHTATTACKKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30,
                                               values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'c', 'x', 'z', 'v',
                                                       'b', 'n', 'm'])
    LIGHTATTACKKey.set('None')
    LIGHTATTACKKey.place(x=250, y=280)
    
    HEAVYATTACKKey = customtkinter.StringVar(None)
    HEAVYATTACKKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30,
                                               values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'c', 'x', 'z', 'v',
                                                       'b', 'n', 'm'])
    HEAVYATTACKKey.set('None')
    HEAVYATTACKKey.place(x=250, y=320)
    
    DodgeKey = customtkinter.StringVar(None)
    DodgeKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30,
                                         values=['None', 'w', 'a', 's', 'd', 'j', 'k', 'l', 'i', 'c', 'x', 'z', 'v', 'b',
                                                 'n', 'm'])
    DodgeKey.set('None')
    DodgeKey.place(x=250, y=360)
    
    JumpKey = customtkinter.StringVar(None)
    JumpKey = customtkinter.CTkComboBox(master=app, corner_radius=10, width=100, height=30,
                                        values=['None', 'up', 'down', 'left', 'right', 'space', 'w', 'a', 's', 'd', 'j',
                                                'k', 'l', 'i', 'c', 'x', 'z', 'v', 'b', 'n', 'm'])
    JumpKey.set('None')
    JumpKey.place(x=250, y=400)
    
    label1 = customtkinter.CTkLabel(master=app, text="Define the keys you use for input in game",
                                    font=('Century Gothic', 25))
    label1.place(x=310, y=50, anchor=tkinter.CENTER)
    
    labelaimup = customtkinter.CTkLabel(master=app, text="Aim Up: ", font=('Century Gothic', 15))
    labelaimup.place(x=160, y=80)
    
    labelaimup2 = customtkinter.CTkLabel(master=app, text="You must bind the Aim Up key in game!",
                                         font=('Century Gothic', 13))
    labelaimup2.place(x=340, y=80)
    
    labelup = customtkinter.CTkLabel(master=app, text="Move Up: ", font=('Century Gothic', 15))
    labelup.place(x=150, y=120)
    
    labelleft = customtkinter.CTkLabel(master=app, text="Move Left: ", font=('Century Gothic', 15))
    labelleft.place(x=150, y=160)
    
    labelright = customtkinter.CTkLabel(master=app, text="Move Right: ", font=('Century Gothic', 15))
    labelright.place(x=150, y=200)
    
    labeldown = customtkinter.CTkLabel(master=app, text="Move Down: ", font=('Century Gothic', 15))
    labeldown.place(x=140, y=240)
    
    labellightattack = customtkinter.CTkLabel(master=app, text="Light Attack: ", font=('Century Gothic', 15))
    labellightattack.place(x=150, y=280)
    
    labelheavyattack = customtkinter.CTkLabel(master=app, text="Heavy Attack: ", font=('Century Gothic', 15))
    labelheavyattack.place(x=135, y=320)
    
    labeldodge = customtkinter.CTkLabel(master=app, text="Dodge/Dash: ", font=('Century Gothic', 15))
    labeldodge.place(x=135, y=360)
    
    labeljump = customtkinter.CTkLabel(master=app, text="Jump: ", font=('Century Gothic', 15))
    labeljump.place(x=175, y=400)
    
    button = customtkinter.CTkButton(master=app, text="Submit", corner_radius=10, width=50, command=submit)
    button.place(x=260, y=445)
    
    app.geometry("+%d+%d" % (x_coordinate, y_coordinate))
    
    if exists('keybinds.json'):
        with open("./keybinds.json") as f:
            keybindData = json.load(f)
        submit(keybindData)
    
    app.mainloop()
    
    Login or Signup to reply.
  2. Just to give an idea, this is an example using two windows inheriting from tkinter.Toplevel.

    import tkinter as tk
    import os
    import json
    
    class MainWindow(tk.Toplevel):
        def __init__(self, master, keybinds):
            super().__init__(master)
            self.title('Main Window')
            self.geometry('300x50')
    
            self.keybinds = keybinds
    
            tk.Label(self, text=f'Keybinds :n{self.keybinds}').pack()
    
            # clicking on the button destroys root and automatically destroys this window
            tk.Button(self, text='Quit', command=self.master.destroy).pack()
    
    class SettingsDialog(tk.Toplevel):
        def __init__(self, master):
            super().__init__(master)
            self.title('Keybinds Settings')
            self.geometry('300x50')
    
            self.keybinds = ''
    
            # load all keybinds widgets here
            tk.Label(self, text='Put all the keybinds widgets herenHere we just set "Jump" to "F1"').pack()
    
            tk.Button(self, text='Submit', command=self.submit).pack()
    
        def submit(self):
            # read the real keybinds from widgets
            self.keybinds = self.create_dummy_keybinds()
            self.destroy()
            
    
        def create_dummy_keybinds(self):
            return {
            "Jump": 'F1'
            }
    
    def main():
    
        root = tk.Tk()
        # hide root window
        root.withdraw()
    
        file_is_present = os.path.isfile('keybinds.json')
    
        if not file_is_present:
            # open settings dialog, activate it and wait for it to close
            settings_dialog = SettingsDialog(root)
            settings_dialog.grab_set()
            settings_dialog.wait_window()
    
            # dialog has been closed - read dialog data and save it to the file
            if settings_dialog.keybinds:
                with open("keybinds.json", "w") as file:
                    json.dump(settings_dialog.keybinds, file)
                file_is_present = True
    
        if file_is_present:
            # read data from file
            key_bind_data = {}
            with open("./keybinds.json") as f:
                key_bind_data = json.load(f)
    
            # display main window passing data as argument
            main_window = MainWindow(root, key_bind_data)        
            main_window.mainloop()
    
        root.mainloop()
    
        
    
    if __name__ == '__main__':    
        main()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search