我正在尝试创建一个python程序来为另一个python程序创建代码。在过去的一周中,我一直在使用一本书(由katie cunningham在24小时内学习python)和许多在线资源来学习python。我在tkinter的OptionMenu中遇到了问题,它没有显示所选的选项或默认选项(我使用了variable.set(“ defaultvalue”))。我知道该程序可能效率很低,所以请对我轻松一点。如果我不提供option.config(width =“ 14”),小部件也将保持很小。(请参见var_create_fn()函数)
ive尝试重新安装pycharm和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()
答案 0 :(得分:0)
您不应使用Tk()
创建对话框窗口。参见Why are multiple instances of Tk discouraged?。
改为使用Toplevel()
:
def var_create_fn():
window2 = Toplevel(window)
...
更新
您的程序建议您熟悉Python,但可能需要Tkinter的帮助,我建议您看看Where to learn tkinter for Python?。
一般来说,对于python,我建议您搜索Python练习并尝试一些,直到找到适合您水平的练习。不过,我还是推荐Python 3 Module of the Week。