Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Spyro543 on August 19, 2011, 05:09:04 pm
-
This program is nowhere near finished, just warning you!
I'm working on a GUI for g++ because I'm bored. I'm having strange problems with it.
from Tkinter import *
import os
import tkFileDialog
root = Tk()
tn = Label(root, text="gcc C++ Compiler GUI")
tn.grid(row=0, column=1, columnspan=3, sticky=N+S+E+W)
pathn = Label(root, text="Path to source:")
pathn.grid(row=1, column=1, sticky=N+S+E+W)
namen = Label(root, text="Name of compiled file:")
namen.grid(row=2, column=1, sticky=N+S+E+W)
path = Entry(root)
path.grid(row=1, column=2, columnspan=2, sticky=N+S+E+W)
path.delete(0, END)
path.insert(0, "")
name = Entry(root)
name.grid(row=2, column=2, columnspan=3, sticky=N+S+E+W)
name.delete(0, END)
name.insert(0, "")
quitbtn = Button(root, text="Exit", fg="red", command=exit)
quitbtn.grid(row=3, column=3, sticky=N+S+E+W)
compilebtn = Button(root, text="Compile!", fg="blue", command=quit)
compilebtn.grid(row=3, column=2, sticky=N+S+E+W)
spyro = Label(root, text="By Spyro543")
spyro.grid(row=3, column=1, sticky=N+S+E+W)
def fopen():
cformats = [('C++ source file','*.cpp'),('Plain text file','*.txt'),('Any File','*.*')]
filenm = tkFileDialog.askopenfile(parent=root,filetypes=cformats ,title='Select the cpp source file')
path.insert(0, filenm)
browsebtn = Button(root, text="Browse", fg="black", command=fopen())
browsebtn.grid(row=1, column=4, sticky=N+S+E+W)
root.mainloop()
I'm having two problems: one is the definde function fopen() is running immediately instead of when I call it (when the button is pressed). Any way to fix this?
def fopen():
cformats = [('C++ source file','*.cpp'),('Plain text file','*.txt'),('Any File','*.*')]
filenm = tkFileDialog.askopenfile(parent=root,filetypes=cformats ,title='Select the cpp source file')
path.insert(0, filenm)
My 2nd problem is that after fopen() runs (when it's not supposed to :mad:) all of the widgets freeze and don't do anything. I can't activate the text boxes and none of the buttons do anything.
-
Bump. Still needing help. >:P
-
This is a typical bug when Tkinter programming.
You use lambda functions.
Change the line from:
browsebtn = Button(root, text="Browse", fg="black", command=fopen())
to
browsebtn = Button(root, text="Browse", fg="black", command=lambda:fopen())
-
Wouldn't it also just work to change it to browsebtn = Button(root, text="Browse", fg="black", command=fopen) ?
You're passing a function, not its result.
Edit: In other words, by using fopen(), you are calling the function.
-
Yes, that works for void functions.
The advantage with lambda is that it allows you to pass arguments
should Spyro543 decide later to do so on fopen:
browsebtn = Button(root, text="Browse", fg="black", command=(lambda: fopen(arg1,arg2,...)))
-
So if fopen() has arguments, I place a "lambda: " infront of it? And if it doesn't, I don't put the () after fopen?
EDIT: One more problem: I get <open file u'C:/Users/Aaron/Downloads/stuff.cpp', mode 'r' at 0x01F85EE8> instead of C:/Users/Aaron/Downloads/stuff.cpp.
-
Change the line in sub fopen to read:
path.insert(0, filenm.name)
instead of:
path.insert(0, filenm)