1.

Solve : python and tk?

Answer»

I'm starting to create a program but when it run an undesired result occurs.
CODE: [SELECT]import sys
from Tkinter import *
import ttk
import tkFileDialog

def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = Tk()
root.title('GUI')
root.geometry('273x450+123+194')
w = GUI (root)
init()
root.mainloop()

w = None
def create_GUI (root):
'''Starting point when module is imported by another program.'''
global w, w_win
if w: # So we have only one INSTANCE of window.
return
w = Toplevel (root)
w.title('GUI')
w.geometry('273x450+123+194')
w_win = GUI (w)
init()
return w_win

def destroy_GUI ():
global w
w.destroy()
w = None
def files():
tkFileDialog.askopenfile(mode='r')



def init():
pass


class GUI:
def __init__(self, master=None):
# Set background of toplevel window to match
# current style
style = ttk.Style()
theme = style.theme_use()
default = style.lookup(theme, 'background')
master.configure(background=default)

self.Entry1 = Entry (master)
self.Entry1.place(relx=0.11,rely=0.09,relheight=0.06,relwidth=0.64)
self.Entry1.configure(background="white")

self.Button1 = Button (master)
self.Button1.place(relx=0.77,rely=0.09,height=27,width=51)
self.Button1.configure(pady="0")
self.Button1.configure(text='''...''')
self.Button1.configure(command=files())







if __name__ == '__main__':
vp_start_gui()
For some reason when you run it the button's command is run and doesn't run when you click it.TheWaffle,

It can help a lot if you indicate what OS you have and which version pf Python. And how does TK fit in here?

Did you copy this code from a sample file?It's because you are CALLING the files command as a parameter. You need to pass the Function object itself. Leave off the parentheses, for example, rather than this:
Code: [Select]self.Button1.configure(command=files())

try this:

Code: [Select]self.Button1.configure(command=files)


Answer to Geek-9pm
windows xp
python 2.7
it came in the msi
NO i didn't copy this it is one i started to writeBut now how can i display the file path in the Entry1 object from the files function?CURRENT CODE:Code: [Select]import sys
from Tkinter import *
import ttk
import tkFileDialog

def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = Tk()
root.title('GUI')
root.geometry('273x450+123+194')
w = GUI (root)
init()
root.mainloop()

w = None
def create_GUI (root):
'''Starting point when module is imported by another program.'''
global w, w_win
if w: # So we have only one instance of window.
return
w = Toplevel (root)
w.title('GUI')
w.geometry('259x244+115+138')
w_win = GUI (w)
init()
return w_win

def destroy_GUI ():
global w
w.destroy()
w = None

def init():
pass

def files():
global fp
fp = tkFileDialog.askopenfile(mode='r')
Entry1.delete(0, END)
Entry1.insert(0, fp)


class GUI:
def __init__(self, master=None):
# Set background of toplevel window to match
# current style
style = ttk.Style()
theme = style.theme_use()
default = style.lookup(theme, 'background')
master.configure(background=default)
global Entry1
Entry1 = Entry (master)
Entry1.place(relx=0.11,rely=0.09,relheight=0.06,relwidth=0.64)
Entry1.configure(background="white")

self.Button1 = Button (master)
self.Button1.place(relx=0.77,rely=0.09,height=27,width=51)
self.Button1.configure(pady="0")
self.Button1.configure(text='''...''')
self.Button1.configure(command=files)


if __name__ == '__main__':
vp_start_gui()
SEE ABOVE POSTYou might want to look up some of the python documentation for 'global'.I looked into that and found my problem but I expanded on my project to create a gui for a python code freezer
for some reason now the RADIO buttons are stuck together when you choose one you choose the one under itCode: [Select]import sys
from Tkinter import *
import ttk
import tkFileDialog
import os
import tkMessageBox



def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = Tk()
root.title('Freezer')
root.geometry('254x244+119+190')
w = Freezer (root)
init()
root.mainloop()

w = None
def create_Freezer (root):
'''Starting point when module is imported by another program.'''
global w, w_win
if w: # So we have only one instance of window.
return
w = Toplevel (root)
w.title('Freezer')
w.geometry('254x244+119+190')
w_win = Freezer (w)
init()
return w_win

def destroy_Freezer ():
global w
w.destroy()
w = None




def init():
pass

def files():
global fp
file_opt = options = {}
options['filetypes'] = [('python files', '.py'), ('python no-console files', '.pyw'), ('all files', '.*')]
options['initialdir'] = 'C:\\'
options['parent'] = root
options['title'] = 'Choose a File'
fp = tkFileDialog.asksaveasfilename(**file_opt)
Entry1.delete(0, END)
Entry1.insert(0, fp)
fp = Entry1.get()
global ol
def rb1():
ol='1'
def rb2():
ol='2'
def rb3():
ol='3'
global conc
def rb4():
conc='console'
def rb5():
conc='windows'
def TB():
pass

class Freezer:
def __init__(self, master=None):
# Set background of toplevel window to match
# current style
style = ttk.Style()
theme = style.theme_use()
default = style.lookup(theme, 'background')
master.configure(background=default)

global Entry1
Entry1 = Entry (master)
Entry1.place(relx=0.12,rely=0.12,relheight=0.11,relwidth=0.69)
Entry1.configure(background="white")

self.Button1 = Button (master)
self.Button1.place(relx=0.79,rely=0.12,height=27,width=31)
self.Button1.configure()
self.Button1.configure(pady="0")
self.Button1.configure(text='''...''', command=files)

global rol
rol=''
self.Radiobutton1 = Radiobutton (master)
self.Radiobutton1.place(relx=0.12,rely=0.45,relheight=0.09
,relwidth=0.13)
self.Radiobutton1.configure(offrelief="groove")
self.Radiobutton1.configure(text='''1''', value=1, variable=rol, command=rb1)
self.Radiobutton1.select()

self.Radiobutton2 = Radiobutton (master)
self.Radiobutton2.place(relx=0.39,rely=0.45,relheight=0.09
,relwidth=0.13)
self.Radiobutton2.configure(offrelief="groove")
self.Radiobutton2.configure(text='''2''', value=2, variable=rol, command=rb2)

self.Radiobutton3 = Radiobutton (master)
self.Radiobutton3.place(relx=0.67,rely=0.45,relheight=0.09
,relwidth=0.13)
self.Radiobutton3.configure(offrelief="groove")
self.Radiobutton3.configure(text='''3''', value=3, variable=rol, command=rb3)

global con
con = ''
self.Radiobutton4 = Radiobutton (master)
self.Radiobutton4.place(relx=0.04,rely=0.66,relheight=0.13
,relwidth=0.41)
self.Radiobutton4.configure(indicatoron="0")
self.Radiobutton4.configure(text='''console''', value=1, variable=con, command=rb4)
self.Radiobutton4.select()

self.Radiobutton5 = Radiobutton (master)
self.Radiobutton5.place(relx=0.55,rely=0.66,relheight=0.13
,relwidth=0.41)
self.Radiobutton5.configure(indicatoron="0")
self.Radiobutton5.configure(text='''no console''', value=2, variable=con, command=rb5)

self.Message1 = Message (master)
self.Message1.place(relx=0.35,rely=0.29,relheight=0.16,relwidth=0.32)
self.Message1.configure(justify="center")
self.Message1.configure(text='''Optimize Level''')

self.TButton1 = ttk.Button (master)
self.TButton1.place(relx=0.35,rely=0.82,height=24,width=67)
self.TButton1.configure(takefocus="")
self.TButton1.configure(text='''Freeze!''')







if __name__ == '__main__':
vp_start_gui()





Discussion

No Comment Found