我尝试用Python编写一个程序,使用customtkinter模拟一个游戏的按键。我已经设置好了,这样在程序开始时,会创建一个.json文件,其中的键由用户在组合框中指定,作为他们在游戏中使用的键。我的问题是,如何才能使它在keybinds.json文件已经存在时,不会创建app = customtkinter.CTk。而是创建“w = customtkinter.CTk”并在那里读取.json文件。
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()
我不知道该尝试什么
1条答案
按热度按时间omhiaaxx1#
If语句在末尾插入并从那里加载,将加载的数据传递给
submit
。然而,app
仍然会被创建和销毁。您必须将代码重构为更小的函数,并适当地调用它们以避免这种情况。