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 2256
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |