Problems programming with Tkinter - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problems programming with Tkinter

SvennglennSvennglenn Posts: 1Member
Hi !

I'm trying to create a graphical program using Tkinter.
The program is supposed to save a string the user defines
as a filename. I've made it work with the the first button
in the program that's named "Spara ett vrde i en strng till ett filnamn", that's swedish for "Save a value in a string as a file name".
The button "Skriv ett vrde till filen" (swedish for "Write a value to the file")

But, i want the program to open a new dialogue when i press the button "Visa ruta" and in that dialogue will be a field where you can enter a value directly in the program, but a can't get it to work because is don't know how i shall do to open a new dialogue window in the program?
Is there someone who can help me with my problem?

Here's the sourcecode for my program:

from Tkinter import *
class App:


def __init__(self, master):

frame = Frame(master)
frame.pack()


self.hi_there = Button(frame, text="Spara ett vrde i en strng till ett filnamn", command=self.say_hi)
self.hi_there.pack(side=LEFT)


self.skriv_varde = Button(frame, text="Skriv ett vrde till filen", command=self.skriv_varde)
self.skriv_varde.pack(side=LEFT)

self.visa_ruta = Button(frame, text="Visa ruta", command=self.visa_ruta)
self.visa_ruta.pack(side=LEFT)

def say_hi(self):
print "Sparar fil"
S = "filen.fil"
output = open(S, 'w')
print "Filen sparad"

def skriv_varde(self):
print "Skriver vrde till filen"
myfile = open('filen.fil', 'w')

myfile.write('Ny mening')
myfile.close()
print "Vrdet till filen skrivet"


def visa_ruta(self, parent):

top = self.top = Toplevel(parent)

Label(top, text="Value").pack()

self.e = Entry(top)
self.e.pack(padx=5)

b = Button(top, text="OK", command=self.ok)
b.pack(pady=5)


def ok(self):

print "value is", self.e.get()

self.top.destroy()


root = Tk()

app = App(root)
root.mainloop()

Comments

  • DrostDrost Posts: 24Member
    [b][red]This message was edited by Drost at 2005-1-3 0:23:4[/red][/b][hr]
    : Hi !
    :

    --- Deliberatelly cut for shortening the message ---

    : But, i want the program to open a new dialogue when i press the button "Visa ruta" and in that dialogue will be a field where you can enter a value directly in the program, but a can't get it to work because is don't know how i shall do to open a new dialogue window in the program?
    : Is there someone who can help me with my problem?
    :
    : Here's the sourcecode for my program:
    :
    : from Tkinter import *
    : class App:
    : def __init__(self, master):
    : frame = Frame(master)
    : frame.pack()
    : self.hi_there = Button(frame, text="Spara ett vrde i en strng till ett filnamn", command=self.say_hi)
    : self.hi_there.pack(side=LEFT)
    : self.skriv_varde = Button(frame, text="Skriv ett vrde till filen", command=self.skriv_varde)
    : self.skriv_varde.pack(side=LEFT)
    : [red]self.visa_ruta[/red] = Button(frame, text="Visa ruta", command=[red]self.visa_ruta[/red])
    : self.visa_ruta.pack(side=LEFT)
    :
    : def say_hi(self):
    : print "Sparar fil"
    : S = "filen.fil"
    : output = open(S, 'w')
    : print "Filen sparad"
    :
    : def skriv_varde(self):
    : print "Skriver vrde till filen"
    : myfile = open('filen.fil', 'w')
    :
    : myfile.write('Ny mening')
    : myfile.close()
    : print "Vrdet till filen skrivet"
    :
    :
    : def [red]visa_ruta[/red](self, parent):
    : top = self.top = Toplevel(parent)
    : Label(top, text="Value").pack()
    : self.e = Entry(top)
    : self.e.pack(padx=5)
    : b = Button(top, text="OK", command=self.ok)
    : b.pack(pady=5)
    :
    : def ok(self):
    : print "value is", self.e.get()
    : self.top.destroy()
    :
    : root = Tk()
    : app = App(root)
    : root.mainloop()

    Maybe a simpler solution is to use the tkSimpleDialog module's askstring function to get the user enter a value (hint: http://www.pythonware.com/library/tkinter/introduction/x1164-data-entry.htm)

    The main problem seems to be a naming error where you assign to self.visa_ruta an instance of a Button widget and also there is a function with the same identifier later (the one which you've told to be the callback function of the button).

    Hope these'll help.

    Drost


Sign In or Register to comment.