i am trying to make a gui and need some help on how to link buttons to certain commands. here is my code: -
from Tkinter import *
-
root =Tk()
-
root.geometry('300x200')
-
root.title('EXAM')
-
Label (text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
-
Entry(root, width=6).pack(side=TOP,padx=10,pady=10)
-
Button(root, text='EXAM').pack(side= TOP, padx=10, pady=10)
-
Button(root, text='Quit').pack(side= BOTTOM, padx=10, pady=10)
-
root.mainloop()
-
when i click on EXAM i want it to do grab the number thats enterd in the entry field so i can use it for further calculations. and when i click on quit i want it to exit the program. how do i go about doing this. and also i want to program to keep running unless i click on quit. for example i might want to enter more bus numbers. and the bus number will always be a 6 digit number so what would be advisable as far as definition. should i define it as an integer or a character or something.
6 2253
Hi, being a newbie myself so take the following with a grain of salt.
Most or many Tkinker objects can 'bind' to a method | function. -
-
def makeTButton(self, text):
-
self.a = TaskButton( self.frm, text = text)
-
self.a.configure(bg = 'green', width = 12)
-
self.a.bind("<ButtonRelease-1>", self.tBtnClicked)
-
self.a.pack()
-
return self.a._name
tBtnClicked being a method
There are several Tkinter references. See Tkinter reference: a GUI for Python from the New Mexico Tech Computer Center. Or see effbot.org
i am trying to make a gui and need some help on how to link buttons to certain commands. here is my code: -
from Tkinter import *
-
root =Tk()
-
root.geometry('300x200')
-
root.title('EXAM')
-
Label (text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
-
Entry(root, width=6).pack(side=TOP,padx=10,pady=10)
-
Button(root, text='EXAM').pack(side= TOP, padx=10, pady=10)
-
Button(root, text='Quit').pack(side= BOTTOM, padx=10, pady=10)
-
root.mainloop()
-
when i click on EXAM i want it to do grab the number thats enterd in the entry field so i can use it for further calculations. and when i click on quit i want it to exit the program. how do i go about doing this. and also i want to program to keep running unless i click on quit. for example i might want to enter more bus numbers. and the bus number will always be a 6 digit number so what would be advisable as far as definition. should i define it as an integer or a character or something.
You need to use the comman attribute of a button: -
def onExam(ent):
-
# do something with the entry
-
-
from Tkinter import *
-
root = Tk()
-
root.geometry('300x200')
-
root.title('EXAM')
-
Label (root, text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
-
ent = Entry(root, width=6)
-
ent.pack(side=TOP,padx=10,pady=10)
-
Button(root, text='EXAM', command = lambda ent=ent: onExam(ent)).pack(side= TOP, padx=10, pady=10)
-
Button(root, text='Quit', command = root.quit()).pack(side= BOTTOM, padx=10, pady=10)
-
root.mainloop()
-
I think it's better with a class though: -
class MyGui(Frame):
-
def __init__(self, parent=None):
-
Frame.__init__(self, parent)
-
-
Label(self, text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
-
self.ent = Entry(self, width=6)
-
self.ent.pack(side=TOP,padx=10,pady=10)
-
Button(self, text='EXAM', command = self.onExam).pack(side= TOP, padx=10, pady=10)
-
Button(self, text='Quit', command = self.quit).pack(side= BOTTOM, padx=10, pady=10)
-
-
def onExam(self):
-
# do something with self.ent
-
ok i went back and redid a lot of the code and decided to go a simpler route. now all i need the program to do is, upon the user pressing enter i want the program to take the 6 digit number in the entry field and use it for further calculations. the program runs ok as far as the gui showing up and all but when i press enter after entering the number in the entry field i get the folloing error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python23\Lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
TypeError: Display() takes exactly 1 argument (2 given)
and also for some reason the program wont quit when i press the Quit button.
Here is the code -
from Tkinter import *
-
import tkMessageBox
-
-
class GUIFramework(Frame):
-
-
-
def __init__(self,master=None):
-
-
Frame.__init__(self,master)
-
-
"""Window Title"""
-
self.master.title("EXAM")
-
self.master.geometry('250x350')
-
-
self.grid(padx=10,pady=10)
-
self.Exam()
-
-
def Exam(self):
-
-
"""Text"""
-
self.lbText = Label(self, text="Enter Bus Number:")
-
self.lbText.grid(row=0, column=1,padx=10,pady=10)
-
-
""" Entry"""
-
self.enText = Entry(self, width=6)
-
self.enText.grid(row=1, column=1, padx=5, pady=5)
-
self.enText.bind("<Return>", self.Display)
-
-
"""Exit Button"""
-
self.btnDisplay = Button(self, text="QUIT",font=( "Impat", "12", "bold"), command=self.exit())
-
self.btnDisplay.grid(row=3, column=1, padx=50, pady=50)
-
-
-
def Display(self):
-
print self.enText.get()
-
-
def exit(self):
-
self.quit()
-
-
-
if __name__ == "__main__":
-
guiFrame = GUIFramework()
-
guiFrame.mainloop()
-
Disregard the previous post. i got the program to do what i wanted to do. now i am almost done with my project. the last and final problem i am having is that the gui wont close when i click on the quit button. I am sure there is a simple and quick fix to this but being a newbie i dont know what it is. when i click on quit the gui just freezes on the screen. here is the code - from Tkinter import *
-
import tkMessageBox
-
-
class GUIFramework(Frame):
-
-
-
def __init__(self,master=None):
-
-
"""Initialise the base class"""
-
Frame.__init__(self,master)
-
-
"""Set the Window Title"""
-
self.master.title("EXAM & POUT")
-
self.master.geometry('200x300')
-
"""Display the main window"""
-
self.grid(padx=10,pady=10)
-
self.lbText = Label(self, text="Enter Bus Number:")
-
self.lbText.grid(row=0, column=1,padx=5,pady=5)
-
-
"""Entry Box"""
-
self.enText = Entry(self, width=6)
-
self.enText.grid(row=1, column=1, padx=5, pady=5)
-
-
"""Button for Running EXAM"""
-
self.btnDisplay = Button(self, text="EXAM",font=( "Impat", "8", "bold"), command=self.exam)
-
self.btnDisplay.grid(row=2, column=1, padx=1, pady=1)
-
-
"""Button for Running POUT"""
-
self.btnDisplay = Button(self, text="POUT",font=( "Impat", "8", "bold"), command=self.pout)
-
self.btnDisplay.grid(row=3, column=1, padx=1, pady=1)
-
-
"""Button for Exiting the Program"""
-
self.btnDisplay = Button(self, text="EXIT",font=( "Impat", "12", "bold"), command=self.exit)
-
self.btnDisplay.grid(row=6, column=1, padx=20, pady=20)
-
-
def exam(self):
-
psspy.report_output(4,"",[0,0])
-
psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
-
psspy.exam(1,0)
-
-
-
def pout(self):
-
psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
-
psspy.pout(1,0)
-
-
def exit(self):
-
global done # This global done i am not sure why it is here i copied some code i found online and this is what it had so i used it. if someone could clarify this it would be much appreciated
-
done = 1 # same thing with this
-
self.destroy()
-
self.quit()
-
-
-
-
if __name__ == "__main__":
-
guiFrame = GUIFramework()
-
guiFrame.mainloop()
Disregard the previous post. i got the program to do what i wanted to do. now i am almost done with my project. the last and final problem i am having is that the gui wont close when i click on the quit button. I am sure there is a simple and quick fix to this but being a newbie i dont know what it is. when i click on quit the gui just freezes on the screen. here is the code - from Tkinter import *
-
import tkMessageBox
-
-
class GUIFramework(Frame):
-
-
-
def __init__(self,master=None):
-
-
"""Initialise the base class"""
-
Frame.__init__(self,master)
-
-
"""Set the Window Title"""
-
self.master.title("EXAM & POUT")
-
self.master.geometry('200x300')
-
"""Display the main window"""
-
self.grid(padx=10,pady=10)
-
self.lbText = Label(self, text="Enter Bus Number:")
-
self.lbText.grid(row=0, column=1,padx=5,pady=5)
-
-
"""Entry Box"""
-
self.enText = Entry(self, width=6)
-
self.enText.grid(row=1, column=1, padx=5, pady=5)
-
-
"""Button for Running EXAM"""
-
self.btnDisplay = Button(self, text="EXAM",font=( "Impat", "8", "bold"), command=self.exam)
-
self.btnDisplay.grid(row=2, column=1, padx=1, pady=1)
-
-
"""Button for Running POUT"""
-
self.btnDisplay = Button(self, text="POUT",font=( "Impat", "8", "bold"), command=self.pout)
-
self.btnDisplay.grid(row=3, column=1, padx=1, pady=1)
-
-
"""Button for Exiting the Program"""
-
self.btnDisplay = Button(self, text="EXIT",font=( "Impat", "12", "bold"), command=self.exit)
-
self.btnDisplay.grid(row=6, column=1, padx=20, pady=20)
-
-
def exam(self):
-
psspy.report_output(4,"",[0,0])
-
psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
-
psspy.exam(1,0)
-
-
-
def pout(self):
-
psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
-
psspy.pout(1,0)
-
-
def exit(self):
-
global done # This global done i am not sure why it is here i copied some code i found online and this is what it had so i used it. if someone could clarify this it would be much appreciated
-
done = 1 # same thing with this
-
self.destroy()
-
self.quit()
-
-
-
-
if __name__ == "__main__":
-
guiFrame = GUIFramework()
-
guiFrame.mainloop()
Try just calling the self.quit() or maybe try self.master.quit(). I don't know about the global done part. Probably the writer of the program set this varaible to 1 when it exited but I don't think you need that.
I have tried self.quit() and self.master.quit() and it just freezes on the screen. What's odd is that it closes when i click on the x in the windows menu bar. Oh well, thanks for all the help i have decided to just not inculde the quit button and just tell the users to use the x to close it.
Thanks again.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Slimick |
last post by:
I want to do a little Tkinter in my 1 credit
python practicum, but I am having problems
getting everything installed correctly.
A sample of the problem is below:
------------------- The...
|
by: corrado |
last post by:
Hello
I have an application running several thread to display some financial
data;
basically I have a thread displaying HTML tables by means of Tkhtml,
another implementing a scrolling ticker...
|
by: syed_saqib_ali |
last post by:
Below is a simple code snippet showing a Tkinter Window bearing a
canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine.
When you shrink/resize the window the scrollbars adjust...
|
by: ishtar2020 |
last post by:
Hi everybody
I'd appreciate some help on creating a tear off menu with TkInter. I've
been reading some documentation but still no luck.
Please don't get confused: when I mean "tear off" menu I...
|
by: Michael Yanowitz |
last post by:
Hello:
Below I have included a stripped down version of the GUI I am working on.
It contains 2 dialog boxes - one main and one settings. It has the following
problems, probably all related, that...
|
by: Edward K. Ream |
last post by:
Hello all,
Creating a 'Help' menu 'by hand' on the Mac does not work, or rather, it
creates a *second* Help menu.
There are hints about how to do this at:
|
by: Eric_Dexter |
last post by:
Instead of creating my buttons and waiting for me to press them to
execute they are executing when I create them and won't do my callback
when I press them.. thanks for any help in advance
...
|
by: alivip |
last post by:
I integrat program to be GUI using Tkinter I try browser direction
as you can see
# a look at the Tkinter Text widget
# use ctrl+c to copy, ctrl+x to cut selected text,
# ctrl+v to...
|
by: Protected |
last post by:
Hello. I'm a complete newbie trying to learn Python. I decided to try
some Tkinter examples, including the one from the library reference,
but they don't seem to do anything! Shouldn't there be,...
|
by: larry |
last post by:
Ok I'm a Python noob, been doing OK so far, working on a data
conversion program and want to create some character image files from
an 8-bit ROM file.
Creating the image I've got down, I open...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |