I have a GUI that I created with python and tkinter.
My code looks something like:
from Tkinter import *
from PIL import Image, ImageTk
master = Tk()
def f1():
print "Change to form A"
def f2():
print "Change to form B"
title = Label(text="Swithing Menus or Forms")
title.grid(row=0, column=1)
blank = Label(text="\n")
blank.grid(row=1, column=1)
b = Button(master, text="Submenu A", command=f1)
b.grid(row=2, column=1)
b2 = Button(master, text="Submenu B", command=f2)
b2.grid(row=2, column=2)
mainloop()
Buttons in the GUI allow me to call functions on click.
I know it is possible to make components inactive, but is it possible to pull up another form upon clicking a button?
Make all components disappear and bring up new ones?
personally to do this I would build each form inside a frame, that way to change forms would be as simple as:
(using pack)
frame1.pack_forget()
frame2.pack()
(using grid)
frame1.grid_forget()
frame2.grid()
and if it was to be a one way thing (ie you cannot reload the first form)
then you can call:
frame.destroy()
James
Ok, firstly James has given a good answer but i'd like to add a couple things:
You can use grid.remove() this will remember where a widget was etc. so you can .grid() it again and get it back with the same specs. This is probably slightly better than pack/grid.forget() as they 'forget' about the widget.
Also to answer the lower part of the question, yes you can bring up new forms, you can create more or different widgets, more windows and even another module can be called within a function.
I think this has answered your question, but if there's anything more just ask.
Related
This question already has answers here:
List of All Tkinter Events
(3 answers)
Closed 2 years ago.
button1=Button(root,text="A1",width=8).grid(row=0,column=0)
button2=Button(root,text="A2",width=8).grid(row=0,column=1)
label1=Label(root,text=" ",padx=20).grid(row=0,column=2)
button22=Button(root,text="A3",width=8).grid(row=0,column=3,sticky='E')
button23=Button(root,text="A4",width=8).grid(row=0,column=4,sticky='E')
I'm trying to make seat arrangement system for a school project. I have an issue: how can I change the colour of the button once it is clicked? I want to change the colour of the booked and available seats once I click on that button.
If you just wish to change the color of a button when clicked, you need to use the .config() method on that button widget.
for example, if a button is defined like
aButton = tk.Button(root, text='change my color').pack()
then to change the color (or pretty much everything related to that widget like text, command or whatever) call the method
aButton.configure(bg='#f0f', fg='#fff') # change to your required colors, bg is background, fg is foreground.
OR .config() can be also be used (these 2 methods do exactly the same)
aButton.config(bg='#f0f', fg='#fff')
Now how will you know when a button is clicked or not. The simplest and intuitive way is to define functions and connect (or bind) them to buttons. Now how you wanna do that is totally up to the user's preferences. Some prefer to create separate different functions for all buttons, some only like to create one.
For your case though, as you don't need to do anything additional than changing colors then single function is enough.
Important In the example code below, I have used lambda functions, a special type of functions, which don't need to be defined separately. That however, by no means, is necessary
Working example for you
from tkinter import * # I don't recommend using global import. better use "import tkinter as tk"
root = Tk()
button1=Button(root,text="A1",width=8, command=lambda: button1.config(bg='#f00'))
button1.grid(row=0,column=0)
button2=Button(root,text="A2",width=8, command=lambda: button2.config(bg='#f00'))
button2.grid(row=0,column=1)
Label(root,text=" ",padx=20).grid(row=0,column=2)
button22=Button(root,text="A3",width=8, command=lambda: button22.config(bg='#f00'))
button22.grid(row=0,column=3,sticky='E')
button23=Button(root,text="A4",width=8, command=lambda: button23.config(bg='#f00'))
button23.grid(row=0,column=4,sticky='E')
root.mainloop()
Using functions
from tkinter import * # I don't recommend using global import. better use "import tkinter as tk"
def changeColor(btn):
# Use your own highlight background argument here instead of bg
btn.configure(bg='#f00')
root = Tk()
button1=Button(root,text="A1",width=8, command=lambda: changeColor(button1))
button1.grid(row=0,column=0)
button2=Button(root,text="A2",width=8, command=lambda: changeColor(button2))
button2.grid(row=0,column=1)
Label(root,text=" ",padx=20).grid(row=0,column=2)
button22=Button(root,text="A3",width=8, command=lambda: changeColor(button22))
button22.grid(row=0,column=3,sticky='E')
button23=Button(root,text="A4",width=8, command=lambda: changeColor(button23))
button23.grid(row=0,column=4,sticky='E')
root.mainloop()
I am trying to create a GUI as shown in the attached picture
I wrote the following code which does the job but not the way I need it to.
try:
import Tkinter as tk
import tkMessageBox as mb
except ImportError:
import tkinter as tk
import tkinter.messagebox as mb
root = tk.Tk()
root.geometry("500x300")
tk.Label(root, text="Python First GUI Template", bg="goldenrod", font="bold").pack()
tk.Label(root, text="").pack()
def addFn():
a = int(input('enter first number'))
b = int(input('enter second number'))
mb.showinfo('showinfo', a+b)
def subtractFn():
a = int(input('enter first number'))
b = int(input('enter second number'))
mb.showinfo('showinfo', a - b)
tk.Button(root, text="Add Function", bg="SkyBlue1", command=addFn).pack()
tk.Label(root, text="").pack()
tk.Button(root, text="Subtract Function", bg="SkyBlue1", command=subtractFn).pack()
root.mainloop()
So, I have the following problems:
(1) I am not able to create the design as I want in the attached picture in terms of relative color and relative location of "add" and "subtract" buttons.
(2) When I hit the buttons to activate "add" or "subtract" functions, the inputs are required on the console. I need a pop up with input box and drop down for two numbers I want to add. I am looking for following format for "add" function.
(3) I want to add a "quit" button to close the console when I am done
I'm new to this myself, and unfortunately can't answer most of your questions, but regarding the quit button, I'm thinking you can write a function that calls quit(), just like you would type in order to exit the Python interactive interpreter. Then you link that function to a button just as you did for the first two buttons. This is the same idea with a lambda expression:
from tkinter import *
root =Tk()
root.geometry("500x300")
Button(root,text="QUIT",bg='Red',command=lambda:(quit())).pack(side=BOTTOM)
root.mainloop()
This here is a TKinter frame that gives you a red quit button at the bottom whose sole reason for existence is to quit the frame it's in.
Regarding the layout, I think the pack method requires you to indicate where pack should prefer to put the widget, but doesn't give you much absolute control. Wouldn't grid method allow for better alignment?
Why do your input boxes have to pop out? Why can't they be embedded into the app frame? I would think that would eliminate some difficulty with the issue, no?
Sorry this isn't the most helpful answer ever... but I hope it gives you something to work with until someone more knowledgeable happens by. Cheers.
There are a couple of ways you could do this, the simplest being using .grid() instead of .pack():
from tkinter import *
root = Tk()
title = Label(root, text="Python First GUI Template")
add = Button(root, text="Add")
subtract = Button(root, text="Subtract")
_quit = Button(root, text="Quit")
title.grid(row=0, column=1, padx=5, pady=5)
add.grid(row=1, column=0, padx=5, pady=5)
subtract.grid(row=1, column=2, padx=5, pady=5)
_quit.grid(row=2, column=3, padx=5, pady=5)
root.mainloop()
.grid() allows you to place widgets on the window in a grid fashion, imagine there are cells which you are placing each widget into, whereas .pack() by default will place items stacked on top of eachother unless certain attributes are given values.
You could also use .place() which allows you to place the widgets based on coordinates but this requires a lot more effort make responsive to window size changes or adding new widgets and the like.
On a side note, Stack Overflow is not a free programming resource, we will not write programs for you based on a list of demands. There are plenty of freelance programmers who are happy to do that in exchange for money. I would recommend in future that rather than asking a question about an incredibly well documented library with over 17000 questions on Stack Overflow, a large number of which are about the difference in the three geometry managers you instead find a tutorial or ask a colleague, schoolmate, teacher or friend for help.
I am a new python user. I'm used to programing on matlab.
I've trying to make a simple GUI with Tkinter pack, but I'm having some problems with that. I had already read and searched what i want but I couldn't develop it.
What I'm trying to do is to make a listbox and when I choose one (or more) options the index be returned (and stored) as a variable (array or vector) that could be used to indexing another array.
The best result I got was a listbox where the index were printed, but not stored as a variable (at least it hasn't been shows in the variables list)
I'm using spyder (anaconda).
I tryied a lot of codes and I don't have this anymore.
Sorry for the dumb question. I guess I still thinking in a Matlab way to write
To keep this application simple, your best option is to get the listbox selection when you want to do something with it:
from tkinter import Tk, Listbox, MULTIPLE, END, Button
def doStuff():
selected = lb.curselection()
if selected: # only do stuff if user made a selection
print(selected)
for index in selected:
print(lb.get(index)) # how you get the value of the selection from a listbox
def clear(lb):
lb.select_clear(0, END) # unselect all
root = Tk()
lb = Listbox(root, selectmode=MULTIPLE) # create Listbox
for n in range(5): lb.insert(END, n) # put nums 0-4 in listbox
lb.pack() # put listbox on window
# notice no parentheses on the function name doStuff
doStuffBtn = Button(root, text='Do Stuff', command=doStuff)
doStuffBtn.pack()
# if you need to add parameters to a function call in the button, use lambda like this
clearBtn = Button(root, text='Clear', command=lambda: clear(lb))
clearBtn.pack()
root.mainloop()
I've also added a button to clear the listbox selection because you cannot unselect items by default.
First, import tkinter, then, create the listbox. Then, you can use curselection to get the contents of the listbox.
import tkinter as tk
root = tk.Tk() #creates the window
myListbox = tk.Listbox(root, select=multiple) #allows you to select multiple things
contentsOfMyListbox = myListbox.curselection(myListbox) #stores selected stuff in tuple
See the documentation here.
I need a widget in TKinter to be a global widget, however, I need the text displayed in it to be different every time. I'm quite new with TKinter and haven't yet successfully managed to edit an option in a widget.
I assume it's something to do with widget.add_option() but the documentation is quite confusing to me and I can't figure out the command.
I specifically just need to edit the text = "" section.
Thanks
EDIT:
gm1_b_current_choice_label = Label(frame_gm1_b, text = "Current input is:\t %s"% str(save_game[6]))
I specifically need to update the save_game[6] (which is a list) in the widget creation, but I assume once the widget is created that's it. I could create the widget every time before I place it but this causes issues with destroying it later.
You can use the .config method to change options on a Tkinter widget.
To demonstrate, consider this simple script:
from Tkinter import Tk, Button, Label
root = Tk()
label = Label(text="This is some text")
label.grid()
def click():
label.config(text="This is different text")
Button(text="Change text", command=click).grid()
root.mainloop()
When the button is clicked, the label's text is changed.
Note that you could also do this:
label["text"] = "This is different text"
or this:
label.configure(text="This is different text")
All three solutions ultimately do the same thing, so you can pick whichever you like.
You can always use the .configure(text = "new text") method, as iCodez suggested.
Alternatively, try using a StringVar as the text_variable parameter:
my_text_var = StringVar(frame_gm1_b)
my_text_var.set("Current input is:\t %s"% str(save_game[6]))
gm1_b_current_choice_label = Label(frame_gm1_b, textvariable = my_text_var)
Then, you can change the text by directly altering my_text_var:
my_text_var.set("Some new text")
This can be linked to a button or another event-based widget, or however else you want to change the text.
I am wondering if it is possible to redirect the output of a function to a tab in the ttk notebook widget.
I assumed it would be similar to the listbox widget where you just used listbox.insert but I can not get this to work.
I apologize if this is a simple question but its really stumped me and I am unable to find any helpful material online to help me.
thanks in advance
Im using python 3.3
The Insert function works almost the same, the only difference is instead of taking a string it takes a frame. Add is a lot simpler though, you don't need to specify an index it just adds it to the end. All you need to do is create a frame, pack a Text element into it, and then pack the whole thing into the notebook. It would look something like this
noteb = ttk.Notebook( root, width=500, height=300 )
frame1 = tkinter.Frame( noteb )
textbox = tkinter.Text( frame1, put whatever you want to put here )
frame1.pack( expand=1, fill='both' )
noteb.add( frame1, whatever parameters you want )
noteb.pack( expand=1, fill='both' )
You should then be able to change the text in textbox directly.
Depends somewhat on what type of widgets you use as tabs, but basically it shouldn't be very different. Just keep track of your tab widgets and call the appropriate method.
Example with two Text tabs:
from Tkinter import *
from ttk import Notebook
def addText(tab):
tab.insert(END, "foo! ")
root = Tk()
nb = Notebook(root, height=240, width=480)
tabs = {"foo": [], "bar": []}
for tabname in tabs:
tab = Text(nb)
tabs[tabname] = tab
nb.add(tab, text= tabname)
nb.pack()
Button(root, text= "Add text!", command = lambda: addText(tabs["foo"])).pack()
root.mainloop()
Clicking the "Add text!" button appends some text to the first tab.