My gui

D

Daniel Kersgaard

Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.

import tkinter
import tkinter.messagebox

class MyGui:
def _init_(self):
self.main_window = tkinter.Tk()

self.top_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)

self.prompt_label = tkinter.Label(self.top_frame, text = 'Entera distance in Kilometers: ')
self.kilo_entry = tkinter.Entry(self.top_frame, width = 10)

self.prompt_label.pack(side = 'left')
self.kilo_entry.pack(side = 'left')

self.calc_button = tkinter.Button(self.bottom_frame, text = 'Convert', command = self.convert)

self.quit_button = tkinter.Button(self.bottom_frame, text = 'Quit', command = self.main_window.destroy)

self.calc_button.pack(side = 'left')
self.quit_button.pack(side = 'left')

self.top_frame.pack()
self.bottom_frame.pack()

tkinter.mainloop()

def convert(self):
kilo = float(self.kilo_entry.get())

miles = kilo * 0.6214

tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is equal to ' + str(miles) + 'miles.')

poop = MyGui()
 
D

Dave Angel

Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.

import tkinter
import tkinter.messagebox

class MyGui:
def _init_(self):
self.main_window = tkinter.Tk()

self.top_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)

self.prompt_label = tkinter.Label(self.top_frame, text = 'Enter a distance in Kilometers: ')
self.kilo_entry = tkinter.Entry(self.top_frame, width = 10)

self.prompt_label.pack(side = 'left')
self.kilo_entry.pack(side = 'left')

self.calc_button = tkinter.Button(self.bottom_frame, text = 'Convert', command = self.convert)

self.quit_button = tkinter.Button(self.bottom_frame, text = 'Quit', command = self.main_window.destroy)

self.calc_button.pack(side = 'left')
self.quit_button.pack(side = 'left')

self.top_frame.pack()
self.bottom_frame.pack()

tkinter.mainloop()

def convert(self):
kilo = float(self.kilo_entry.get())

miles = kilo * 0.6214

tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is equal to ' + str(miles) + 'miles.')

poop = MyGui()

I'm not an IDLE user, but I wouldn't be surprised if IDLE interferes
with some GUI apps. I'd run your code from the bash prompt.
 
C

Chris “Kwpolska†Warrick

Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Hereis the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.

import tkinter
import tkinter.messagebox

class MyGui:
def _init_(self):
self.main_window = tkinter.Tk()

self.top_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)

self.prompt_label = tkinter.Label(self.top_frame, text = 'Enter a distance in Kilometers: ')
self.kilo_entry = tkinter.Entry(self.top_frame, width = 10)

self.prompt_label.pack(side = 'left')
self.kilo_entry.pack(side = 'left')

self.calc_button = tkinter.Button(self.bottom_frame, text = 'Convert', command = self.convert)

self.quit_button = tkinter.Button(self.bottom_frame, text = 'Quit', command = self.main_window.destroy)

self.calc_button.pack(side = 'left')
self.quit_button.pack(side = 'left')

self.top_frame.pack()
self.bottom_frame.pack()

tkinter.mainloop()

def convert(self):
kilo = float(self.kilo_entry.get())

miles = kilo * 0.6214

tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers isequal to ' + str(miles) + 'miles.')

poop = MyGui()

poop? Seriously? You aren’t serious about that copying, right?

Your code seems to be missing a lot of important stuff. You don’t
inherit from tkinter.Frame. Compare your program to the sample “Hello
world!†program:
http://docs.python.org/2/library/tkinter.html#a-simple-hello-world-program
— unfortunately, I am not a fan and I am not knowledgeable about
tkinter, but I can see a lot of stuff is missing.

Please try fixing it and running it _outside of IDLE_, which is also
built in Tk. This may cause problems (but don’t worry, the code you
pasted doesn’t work anyways.)
 
N

Ned Batchelder

Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.

import tkinter
import tkinter.messagebox

class MyGui:
def_init_(self):

You need to define __init__, not _init_. Python special methods are
named with double underscore leading and trailing, and __x__ is
pronounced "dunder-x".

--Ned.
 
A

Arnaud Delobelle

poop? Seriously? You aren’t serious about that copying, right?

Your code seems to be missing a lot of important stuff. You don’t
inherit from tkinter.Frame. Compare your program to the sample “Hello
world!†program:

His class is not a frame, it's just a container for the tkinter code.
It's a bit unusual but it looks correct to me (apart from the single
underscores in __init__() as spotted by Ned Batchelder).
 
N

Neil Cerutti

His class is not a frame, it's just a container for the tkinter
code. It's a bit unusual but it looks correct to me (apart from
the single underscores in __init__() as spotted by Ned
Batchelder).

I dunno if it makes any difference, but it's usual to call
mainloop of the root window, rather than tkinter.mainloop.
 
T

Terry Jan Reedy

Please try fixing it and running it _outside of IDLE_, which is also
built in Tk

The default mode of Idle runs user code in a separate process. Editing
tkinter code with Idle and running it (in the separate process) should
be no problem.
 
C

Chris Angelico

import tkinter
import tkinter.messagebox

class MyGui:
def _init_(self):
... all code here

poop = MyGui()

As already mentioned, changing that to __init__ makes everything work
(I just tested in Python 3.3 on Windows). But since Python is not
Java, there's no reason to bury all this code in a class; just move
all that code flush left and abandon the explicit instantiation. The
constructor doesn't return until the window's been closed, so there's
really no point in returning an object. (Maybe if you remove the
mainloop() call, you could possibly make use of two of these sorts of
windows, but YAGNI - don't bother with the complexity required to
handle multiple simultaneous windows until you have a use-case.)

So here's a direct translation to top-level code:

import tkinter
import tkinter.messagebox

def convert():
kilo = float(kilo_entry.get())
miles = kilo * 0.6214
tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is
equal to ' + str(miles) + 'miles.')

main_window = tkinter.Tk()

top_frame = tkinter.Frame(main_window)
bottom_frame = tkinter.Frame(main_window)

prompt_label = tkinter.Label(top_frame, text = 'Enter a distance in
Kilometers: ')
kilo_entry = tkinter.Entry(top_frame, width = 10)

prompt_label.pack(side = 'left')
kilo_entry.pack(side = 'left')

calc_button = tkinter.Button(bottom_frame, text = 'Convert', command = convert)

quit_button = tkinter.Button(bottom_frame, text = 'Quit', command =
main_window.destroy)

calc_button.pack(side = 'left')
quit_button.pack(side = 'left')

top_frame.pack()
bottom_frame.pack()

tkinter.mainloop()


-- cut --

(You may want to bury the code inside a main(), but that's optional too.)

And here's a slightly modified version:

import tkinter
import tkinter.messagebox

main_window = tkinter.Tk()
top_frame = tkinter.Frame(main_window)
bottom_frame = tkinter.Frame(main_window)

tkinter.Label(top_frame, text = 'Enter a distance in Kilometers:
').pack(side = 'left')
km_entry = tkinter.Entry(top_frame, width = 10); km_entry.pack(side = 'left')

def convert():
km = float(km_entry.get())
miles = km * 0.6214
tkinter.messagebox.showinfo('Result', '%.2f kilometers is equal to
%.2f miles.' % (km, miles) )

tkinter.Button(bottom_frame, text = 'Convert', command =
convert).pack(side = 'left')
tkinter.Button(bottom_frame, text = 'Quit', command =
main_window.destroy).pack(side = 'left')

top_frame.pack()
bottom_frame.pack()

tkinter.mainloop()

-- cut --

I've removed some redundant variables (you don't need to hang onto
references to your labels,just pack 'em in and be done), and reordered
the code a bit to put the Convert button's function right near where
the Convert button is created (take your pick - do you prefer that, or
to have all the other functions up top? Both are viable); I also
changed your message box to use percent-formatting with fixed decimals
rather than str(), to tidy up the display a bit. Oh, and renamed
"kilo" to "km", because to me "kilo" means "kilogram". :)

This version of the code isn't necessarily better in every way, but
it's different. Like the Oracle in The Matrix, I want you to make up
your own mind as to what you ought to do; maybe you'll like some of my
changes, maybe you won't, but either way you're making an intelligent
decision about code style :)

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top