473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with Tkinter

i am trying to make a gui and need some help on how to link buttons to certain commands. here is my code:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. root =Tk()
  3. root.geometry('300x200')
  4. root.title('EXAM')
  5. Label (text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
  6. Entry(root, width=6).pack(side=TOP,padx=10,pady=10)
  7. Button(root, text='EXAM').pack(side= TOP, padx=10, pady=10)
  8. Button(root, text='Quit').pack(side= BOTTOM, padx=10, pady=10)
  9. root.mainloop()
  10.  
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.
Oct 15 '07 #1
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     def makeTButton(self, text):
  3.         self.a = TaskButton( self.frm, text = text)
  4.         self.a.configure(bg = 'green', width = 12)
  5.         self.a.bind("<ButtonRelease-1>", self.tBtnClicked
  6.         self.a.pack()
  7.         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
Oct 15 '07 #2
ilikepython
844 Expert 512MB
i am trying to make a gui and need some help on how to link buttons to certain commands. here is my code:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. root =Tk()
  3. root.geometry('300x200')
  4. root.title('EXAM')
  5. Label (text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
  6. Entry(root, width=6).pack(side=TOP,padx=10,pady=10)
  7. Button(root, text='EXAM').pack(side= TOP, padx=10, pady=10)
  8. Button(root, text='Quit').pack(side= BOTTOM, padx=10, pady=10)
  9. root.mainloop()
  10.  
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:
Expand|Select|Wrap|Line Numbers
  1. def onExam(ent):
  2.     # do something with the entry
  3.  
  4. from Tkinter import *
  5. root = Tk()
  6. root.geometry('300x200')
  7. root.title('EXAM')
  8. Label (root, text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
  9. ent = Entry(root, width=6)
  10. ent.pack(side=TOP,padx=10,pady=10)
  11. Button(root, text='EXAM', command = lambda ent=ent: onExam(ent)).pack(side= TOP, padx=10, pady=10)
  12. Button(root, text='Quit', command = root.quit()).pack(side= BOTTOM, padx=10, pady=10)
  13. root.mainloop()
  14.  
I think it's better with a class though:
Expand|Select|Wrap|Line Numbers
  1. class MyGui(Frame):
  2.     def __init__(self, parent=None):
  3.         Frame.__init__(self, parent)
  4.  
  5.         Label(self, text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
  6.         self.ent = Entry(self, width=6)
  7.         self.ent.pack(side=TOP,padx=10,pady=10)
  8.         Button(self, text='EXAM', command = self.onExam).pack(side= TOP, padx=10, pady=10)
  9.         Button(self, text='Quit', command = self.quit).pack(side= BOTTOM, padx=10, pady=10)
  10.  
  11.     def onExam(self):
  12.         # do something with self.ent
  13.  
Oct 15 '07 #3
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
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkMessageBox
  3.  
  4. class GUIFramework(Frame):
  5.  
  6.  
  7.     def __init__(self,master=None):
  8.  
  9.         Frame.__init__(self,master)
  10.  
  11.         """Window Title"""
  12.         self.master.title("EXAM")
  13.         self.master.geometry('250x350')
  14.  
  15.         self.grid(padx=10,pady=10)
  16.         self.Exam()
  17.  
  18.     def Exam(self):
  19.  
  20.         """Text"""
  21.         self.lbText = Label(self, text="Enter Bus Number:")
  22.         self.lbText.grid(row=0, column=1,padx=10,pady=10)
  23.  
  24.         """ Entry"""
  25.         self.enText = Entry(self, width=6)
  26.         self.enText.grid(row=1, column=1, padx=5, pady=5)
  27.         self.enText.bind("<Return>", self.Display)  
  28.  
  29.         """Exit Button"""
  30.         self.btnDisplay = Button(self, text="QUIT",font=( "Impat", "12", "bold"), command=self.exit())
  31.         self.btnDisplay.grid(row=3, column=1, padx=50, pady=50)
  32.  
  33.  
  34.     def Display(self):
  35.         print self.enText.get()
  36.  
  37.     def exit(self):
  38.         self.quit()
  39.  
  40.  
  41. if __name__ == "__main__":
  42.     guiFrame = GUIFramework()
  43.     guiFrame.mainloop()
  44.  
Oct 16 '07 #4
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

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkMessageBox
  3.  
  4. class GUIFramework(Frame):
  5.  
  6.  
  7.     def __init__(self,master=None):
  8.  
  9.         """Initialise the base class"""
  10.         Frame.__init__(self,master)
  11.  
  12.         """Set the Window Title"""
  13.         self.master.title("EXAM & POUT")
  14.         self.master.geometry('200x300')
  15.         """Display the main window"""
  16.         self.grid(padx=10,pady=10)
  17.         self.lbText = Label(self, text="Enter Bus Number:")
  18.         self.lbText.grid(row=0, column=1,padx=5,pady=5)
  19.  
  20.         """Entry Box"""
  21.         self.enText = Entry(self, width=6)
  22.         self.enText.grid(row=1, column=1, padx=5, pady=5)
  23.  
  24.         """Button for Running EXAM"""
  25.         self.btnDisplay = Button(self, text="EXAM",font=( "Impat", "8", "bold"), command=self.exam)
  26.         self.btnDisplay.grid(row=2, column=1, padx=1, pady=1)
  27.  
  28.         """Button for Running POUT"""
  29.         self.btnDisplay = Button(self, text="POUT",font=( "Impat", "8", "bold"), command=self.pout)
  30.         self.btnDisplay.grid(row=3, column=1, padx=1, pady=1)
  31.  
  32.         """Button for Exiting the Program"""
  33.         self.btnDisplay = Button(self, text="EXIT",font=( "Impat", "12", "bold"), command=self.exit)
  34.         self.btnDisplay.grid(row=6, column=1, padx=20, pady=20)
  35.  
  36.     def exam(self):
  37.         psspy.report_output(4,"",[0,0])
  38.         psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
  39.         psspy.exam(1,0)
  40.  
  41.  
  42.     def pout(self):
  43.         psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
  44.         psspy.pout(1,0)
  45.  
  46.     def exit(self):
  47.         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
  48.         done = 1 # same thing with this 
  49.         self.destroy()
  50.         self.quit()
  51.  
  52.  
  53.  
  54. if __name__ == "__main__":
  55.     guiFrame = GUIFramework()
  56.     guiFrame.mainloop()
Oct 16 '07 #5
ilikepython
844 Expert 512MB
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

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkMessageBox
  3.  
  4. class GUIFramework(Frame):
  5.  
  6.  
  7.     def __init__(self,master=None):
  8.  
  9.         """Initialise the base class"""
  10.         Frame.__init__(self,master)
  11.  
  12.         """Set the Window Title"""
  13.         self.master.title("EXAM & POUT")
  14.         self.master.geometry('200x300')
  15.         """Display the main window"""
  16.         self.grid(padx=10,pady=10)
  17.         self.lbText = Label(self, text="Enter Bus Number:")
  18.         self.lbText.grid(row=0, column=1,padx=5,pady=5)
  19.  
  20.         """Entry Box"""
  21.         self.enText = Entry(self, width=6)
  22.         self.enText.grid(row=1, column=1, padx=5, pady=5)
  23.  
  24.         """Button for Running EXAM"""
  25.         self.btnDisplay = Button(self, text="EXAM",font=( "Impat", "8", "bold"), command=self.exam)
  26.         self.btnDisplay.grid(row=2, column=1, padx=1, pady=1)
  27.  
  28.         """Button for Running POUT"""
  29.         self.btnDisplay = Button(self, text="POUT",font=( "Impat", "8", "bold"), command=self.pout)
  30.         self.btnDisplay.grid(row=3, column=1, padx=1, pady=1)
  31.  
  32.         """Button for Exiting the Program"""
  33.         self.btnDisplay = Button(self, text="EXIT",font=( "Impat", "12", "bold"), command=self.exit)
  34.         self.btnDisplay.grid(row=6, column=1, padx=20, pady=20)
  35.  
  36.     def exam(self):
  37.         psspy.report_output(4,"",[0,0])
  38.         psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
  39.         psspy.exam(1,0)
  40.  
  41.  
  42.     def pout(self):
  43.         psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
  44.         psspy.pout(1,0)
  45.  
  46.     def exit(self):
  47.         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
  48.         done = 1 # same thing with this 
  49.         self.destroy()
  50.         self.quit()
  51.  
  52.  
  53.  
  54. if __name__ == "__main__":
  55.     guiFrame = GUIFramework()
  56.     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.
Oct 16 '07 #6
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.
Oct 17 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
1
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...
0
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...
2
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...
1
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...
4
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:
6
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 ...
1
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...
7
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,...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.