Okay, ich habe es nun entfernt. Und können Sie mir sagen wie man es schafft, ohne das der vorhandene Datei-Text entfernt wird? Also es wird nur der letzte Befehl geschrieben.
Code: Alles auswählen
from tkinter import *
import os
# __________________
#___/Befhele definieren\_______________________________________
def forward():
with open('code.txt', 'w') as outfile:
print("roboter.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse, 50);", file=outfile)
print("roboter.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse, 50);\n", file=outfile)
def reverse():
with open('code.txt', 'w') as outfile:
print("roboter.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward, 50);", file=outfile)
print("roboter.bank_a.motorRunUnlimited(SH_Motor_2, SH_Direction_Forward, 50);\n", file=outfile)
def off():
with open('code.txt', 'w') as outfile:
print("roboter.bank_a.motorStop(SH_Motor_1, SH_Next_Action_Brake);")
print("roboter.bank_a.motorStop(SH_Motor_2, SH_Next_Action_Brake);\n")
def setint():
setint_name = input("Name der Variable:")
setint_val = input("Wert der Variable:")
with open('code.txt', 'w') as outfile:
print(setint_name, "=", setint_val, ";", file=outfile)
def makeint():
makeint_name = input("Name der Variable:")
with open('code.txt', 'w') as outfile:
print("int",makeint_name,";", file=outfile)
def getVal():
sensor_name = input("Welcher Sensor soll gelesen Werden?")
with open('code.txt', 'w') as outfile:
print(sensor_name,".getVal();", file=outfile)
def define():
define_name = input("Name der Funktion")
define_action = input("Was soll definiert werden?")
with open('code.txt', 'w') as outfile:
print("#define",define_name,define_action,";", file=outfile)
def delay():
delay_dur = input("Wie lange soll gewartet werden (ms)?")
with open('code.txt', 'w') as outfile:
print("delay(",delay_dur,");", file=outfile)
def ifask():
ifcon = input("Was soll die Bedingung sein?")
with open('code.txt', 'w') as outfile:
print("if(",ifcon,"){", file=outfile)
def endif():
with open('code.txt', 'w') as outfile:
print("}", file=outfile)
def whilestart():
whilecon = input("Was soll die Bedingung sein?")
with open('code.txt', 'w') as outfile:
print("while(",whilecon,"){", file=outfile)
def whileend():
with open('code.txt', 'w') as outfile:
print("}", file=outfile)
def serialbegin():
with open('code.txt', 'w') as outfile:
print("Serial.begin(9600);", file=outfile)
def serialln():
serial_str = input("What should be printed on the Serial Monitor?:")
with open('code.txt', 'w') as outfile:
print('Serial.println("',serial_str,'")', file=outfile)
# ______________________
#___/root-fesnter erstellen\_______________________________________
root = Tk()
root.title("TexttoArduino")
root.resizable(width=False, height=False)
os.system("clear")
# ___________________________________
#___/Buttons, Label, Variablen erstellen\__________________________
entry = Entry(root, width=20)
entryvar = entry.get()
lbl_commands = Label(root, text="Befehle:\n")
btn_forward = Button(root, text="Vorwärts bewegen", width=17, command=forward)
btn_reverse = Button(root, text="Ruckwärts bewegen", width=17, command=reverse)
btn_off = Button(root, text="Motoren stoppen", width=17, command=off)
btn_setint = Button(root, text="Variable verändern", width=17, command=setint)
btn_makeint = Button(root, text="Veriable erstellen", width=17, command=makeint)
btn_getval = Button(root, text="Sensorwerte auslesen", width=17, command=getVal)
btn_define = Button(root, text="Befehl definieren", width=17, command=define)
btn_delay = Button(root, text="Warten", width=17, command=delay)
btn_if = Button(root, text="Falls", width=17, command=ifask)
btn_endif = Button(root, text="X", width=17, command=endif)
btn_while = Button(root, text="Wiederholen", width=17, command=whilestart)
btn_endwhile = Button(root, text="X", width=17, command=whileend)
lbl_serial = Label(root, text="Serieller Monitor:\n", width=17)
btn_serialbegin = Button(root, text="SM starten", width=17, command=serialbegin)
btn_serialln = Button(root, text="Text auf SM ausgeben", width=17, command=serialln)
# __________________________
#___/set the color of the boxes\___________________________________
btn_endif["fg"] = "#F0F0F0"
btn_endwhile["fg"] = "#F0F0F0"
# ___________________________________
#___/Buttons/Labels auf ein Grid setzten\__________________________
entry.grid(row=1, column=1)
lbl_commands.grid(row=2, column=2)
btn_forward.grid(row=3, column=1, pady=2, padx=2)
btn_reverse.grid(row=3, column=2, pady=2, padx=2)
btn_off.grid(row=3, column=3, pady=2, padx=2)
btn_makeint.grid(row=4, column=1, pady=2, padx=2)
btn_setint.grid(row=4, column=2, pady=2, padx=2)
btn_getval.grid(row=4, column=3, pady=2, padx=2)
btn_define.grid(row=5, column=1, pady=2, padx=2)
btn_delay.grid(row=5, column=2, pady=2, padx=2)
btn_if.grid(row=6, column=1, pady=2, padx=2)
btn_endif.grid(row=6, column=2)
btn_while.grid(row=7, column=1, pady=2, padx=2)
btn_endwhile.grid(row=7, column=2)
lbl_serial.grid(row=8, column=2)
btn_serialbegin.grid(row=9, column=1, pady=2, padx=2)
btn_serialln.grid(row=9, column=2, pady=2, padx=2)
# _________________________
#___/mainloop für root starten\____________________________________
entry.focus_set()
root.mainloop()