473,289 Members | 1,756 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,289 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 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.
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: 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"....
0
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...
0
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 =...
0
isladogs
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...
0
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...
1
isladogs
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...
0
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...
0
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...
1
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)...

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.