0

I am trying to create a python program to create the code for another python program. i have been learning python over the past week using a book (learn python in 24 hrs by katie cunningham) and also numerous online sources. i have run into a problem with the OptionMenu in tkinter where, it does not show the selected option or the default option (i used variable.set("defaultvalue")). i know that the program might be very inefficient, so please go easy on me. also if i dont provide option.config(width="14") the widget stays small.(please see the var_create_fn() function)

ive tried reinstalling pycharm and also python.

from tkinter import *
# from PIL import Image,ImageTk
from tkinter.messagebox import showinfo

window = Tk()
window.geometry('650x520')
window.title("Sash's CodeBuilder (feat.Python)")
main_label = Label(window, text="CodeBuilder Alpha", fg="indigo", bg="white", relief=SOLID, font=("arial", 30, "bold", "italic"))
main_label.pack(fill=BOTH, padx=3, pady=3)

# Variable declarations
printable = StringVar()
variable = StringVar()
varVal = StringVar()
varTypeSet = StringVar()
varTypes = ["String", "Integer"]
dataTypes = ["int", "float", "String", "list", "eval"]
dataTypeSet = StringVar()
inText = StringVar()
inVar = StringVar()
forVar = StringVar()
forPar = StringVar()
rangeChk = IntVar()


# FUNCTIONS
def printer():
    showinfo(title="Printer", message="Printed.")
    code = open("D:\\file.py", "a")
    code.write("print(" + printable.get() + ")") & code.write("\n")


def var_create_fn():
    window2 = Tk()
    window2.geometry('150x100')
    window2.title("Data Type Select")
    window2_label = Label(window2, text="Select a data type :", bg="white", relief=SOLID, font=("arial", 10))
    window2_label.pack(fill=BOTH, padx=3, pady=3)
    var_data_type_box = OptionMenu(window2, varTypeSet, *varTypes)
    var_data_type_box.config(width="14")
    varTypeSet.set("select data type")
    var_data_type_box.place(x=10, y=30)
    but = Button(window2, text="Print", command=var_create_sub, font=("arial", 9, "bold"), bg="#44ff4c", fg="Blue")
    but.place(x=55, y=70)
    window2.mainloop()


def var_create():
    if varVal.get() == "Enter variable value":
        var_create_fn()
    else:
        showinfo(title="Variable creation", message="Variable with specified value created")
        code = open("D:\\file.py", "a")
        code.write(variable.get() + "=" + varVal.get()) & code.write("\n")


def var_create_sub():
    global var1
    var1 = varTypeSet.get()
    if var1 == "String":
        showinfo(title="Variable creation", message="Variable with specified value created")
        code = open("D:\\file.py", "a")
        code.write(variable.get() + " = StringVar()") & code.write("\n")
    elif var1 == "Integer":
        showinfo(title="Variable creation", message="Variable with specified value created")
        code = open("D:\\file.py", "a")
        code.write(variable.get() + " = IntVar()") & code.write("\n")
        exit()
    else:
        showinfo(title="Variable creation", message="please select a value :")


def in_create():
    create_data_type = dataTypeSet.get()
    if create_data_type == "Select Data type":
        showinfo(title="error", message="Empty field")
    elif create_data_type != "String":
        showinfo(title="Input creation", message="Input Code,, with specified data type and variable created")
        code = open("D:\\file.py", "a")
        code.write(inVar.get() + "=" + create_data_type+'(input("'+inText.get()+'"))') & code.write("\n")
    else:
        showinfo(title="Input creation", message="Input Code,, with specified data type and variable created")
        code = open("D:\\file.py", "a")
        code.write(inVar.get() + '=input("' + inText.get() + '")') & code.write("\n")


def for_create():
    showinfo(title="For loop created", message="For loop with specified variable and parameters created.")
    if rangeChk.get() == 1:
        code = open("D:\\file.py", "a")
        code.write("for " + forVar.get() + " in range(" + forPar.get() + "):") & code.write("\n")
    else:
        code = open("D:\\file.py", "a")
        code.write("for " + forVar.get() + " in " + forPar.get() + ":") & code.write("\n")


# PROGRAM EXIT
btx = Button(window, text="Exit", command=exit, relief=SOLID, font=("arial", 14, "bold"))
btx.place(x=300, y=470)

# FOR PRINTING SOMETHING
# print label
printLabel = Label(window, text="Enter text to print :", bg="white", relief=SOLID, font=("arial", 10))
printLabel.place(x=5, y=58)

# text box
printBox = Entry(window, textvar=printable)
printBox.place(x=124, y=60)

# Print button
bt1 = Button(window, text="Print", command=printer, relief=RIDGE, font=("arial", 9, "bold"), bg="#44ff4c", fg="Blue")
bt1.place(x=250, y=57)


# CREATING A VARIABLE
# Variable label
varLabel = Label(window, text="Create a variable :", bg="white", relief=SOLID, font=("arial", 10))
varLabel.place(x=5, y=84)

# Variable Box
varBox = Entry(window, textvar=variable)
varBox.insert(END, "Enter variable name")
varBox.place(x=124, y=87)

# Variable value box
varValBox = Entry(window, textvar=varVal)
varValBox.insert(0, "Enter variable value")
varValBox.place(x=254, y=87)

# Variable Button
bt2 = Button(window, text="Create", relief=RIDGE, command=var_create, font=("arial", 9, "bold"), bg="#44ff4c", fg="Blue")
bt2.place(x=380, y=82)

# CREATING INPUT CODE
# Input label
inLabel = Label(window, text="Create input Code:", bg="white", relief=SOLID, font=("arial", 10))
inLabel.place(x=5, y=110)

# Data type combo box
inDataType = OptionMenu(window, dataTypeSet, *dataTypes)
dataTypeSet.set("Select Data type")
inDataType.config(width="14")
inDataType.place(x=122, y=107)

# Input text box
inTextBox = Entry(window, textvar=inText)
inTextBox.insert(0, "Enter input text")
inTextBox.place(x=254, y=113)

# Input variable box
inVarBox = Entry(window, textvar=inVar)
inVarBox.insert(0, "Enter input variable")
inVarBox.place(x=382, y=113)

# Input Button
b3 = Button(window, text="Create", relief=RIDGE, command=in_create, font=("arial", 9, "bold"), bg="#44ff4c", fg="Blue")
b3.place(x=508, y=110)

# FOR LOOP
# For Loop label
forLabel = Label(window, text=" Create For loop:", bg="white", relief=SOLID, font=("arial", 10))
forLabel.place(x=5, y=136)

# For Variable box
forVarBox = Entry(window, textvar=forVar)
forVarBox.insert(0, "Enter loop variable")
forVarBox.place(x=124, y=139)

# For Loop parameters box
forParBox = Entry(window, textvar=forPar)
forParBox.insert(0, "Enter loop parameters")
forParBox.place(x=254, y=139)

# for Button
b4 = Button(window, text="Create", relief=RIDGE, bg="#44ff4c", fg="Blue", command=for_create, font=("arial", 9, "bold"))
b4.place(x=382, y=136)

# range check box
rangeChkBut = Checkbutton(window, text="use range", onvalue=1, offvalue=0, variable=rangeChk)
rangeChkBut.place(x=442, y=138)

window.mainloop()

1 Answers1

0

You should not use Tk() to create the dialog window. See Why are multiple instances of Tk discouraged?.

Use Toplevel() instead:

def var_create_fn():
    window2 = Toplevel(window)
    ...

Update

Your program suggests that you are familiar with Python but may need help with Tkinter I suggest you look at Where to learn tkinter for Python?.

For python in general I suggest you search for Python exercises and try out a few until you find one that suits your level. I do, however, recommend Python 3 Module of the Week.

figbeam
  • 7,001
  • 2
  • 12
  • 18
  • thanks a lot figbeam , that has solved the problem. i also went thru the forum link u sent me. i understand now. i have another question. what resources(online) would you suggest for me to learn python more effectively ? – Tony Stark Jul 21 '19 at 10:43