Unable to place the buttons at required positions using tkinter - python

I tried the below code but the buttons are placed only at the center only,it is not placed according to my required positioning
from Tkinter import *
import tkFileDialog
from PIL import ImageTk, Image
root = Tk()
def DT(event):
print "Decision tree is selected"
button3 = Button(root, text="Decision Tree",font=("Helvetica", 15))
button3.place(x=50,y=220)
button3.pack()
button3.bind('<Button-1>', DT)
root.minsize(width=1300, height=700)
#root.configure(background='lavender')
root.mainloop()

button3.pack() overrides the placement made by button3.place().
Delete the button3.pack() line.

You are placing and packing the button. Just use either place or pack.

Related

Tkinter toplevel window is not defined

I wonder if someone could tell me if its possible to update toplevel windows using external functions. I've replicated my issue below what I need to do is update the Toplevel(master) using the function updatelabel(). I have used similar external function to update items in root which works like a dream. However, with the top level window I always get the
NameError: name 'newWindow' is not defined
The only work around I found was to kill the newWindow using newWindow.destroy() on each load but this method makes the screen pop up and then close again which doesn't look pretty. Any help most welcome thanks.
from tkinter import *
from tkinter.ttk import *
master = Tk()
master.geometry("200x200")
def updatelabel():
Label(newWindow,
text="I changed").pack()
def openNewWindow():
# Toplevel object which will
# be treated as a new window
newWindow = Toplevel(master)
# sets the title of the
# Toplevel widget
newWindow.title("New Window")
# sets the geometry of toplevel
newWindow.geometry("200x200")
# A Label widget to show in toplevel
Label(newWindow,
text="I want to change").pack()
button1 = Button(newWindow,
text="Click me to change label", command=updatelabel).pack()
btn = Button(master,
text="open a new window",
command=openNewWindow)
btn.pack(pady=10)
mainloop()
Your “newWindow” is defined in your “openNewWindow” function and so it basically only exists in there, you could probably fix this by either defining “newWindow” outside of the function, or by using it as an argument(just add it to the brackets and give it a name in the function itself’s brackets) calling “updateLabel”
I think this should work, though I haven’t worked with tkinter in a bit so don’t blame me if it doesn’t
from tkinter import *
from tkinter.ttk import *
master = Tk()
master.geometry("200x200")
def updatelabel(newWindow):
Label(newWindow,
text="I changed").pack()
def openNewWindow():
# Toplevel object which will
# be treated as a new window
newWindow = Toplevel(master)
# sets the title of the
# Toplevel widget
newWindow.title("New Window")
# sets the geometry of toplevel
newWindow.geometry("200x200")
# A Label widget to show in toplevel
Label(newWindow,
text="I want to change").pack()
button1 = Button(newWindow,
text="Click me to change label", command= lambda: updatelabel(newWindow)).pack()
btn = Button(master,
text="open a new window",
command=openNewWindow)
btn.pack(pady=10)
mainloop()

Is there away to hide the text inside a button/label in Tkinter?

I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.
Let say I create a simple button like this:
import tkinter as tk
window = tk.Tk()
button = tk.Button(text="Hello World!")
button.pack()
window.mainloop()
Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead.
Any help would be appreciated.
To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.
import tkinter as tk
def hide_text():
color = button['bg']
button.config(foreground=color, activeforeground=color)
window = tk.Tk()
button = tk.Button(text="Hello World!",command=hide_text)
button.pack()
window.mainloop()
Approach 2 ttk.Button:
import tkinter as tk
from tkinter import ttk
def hide_text():
button.config(text='')
window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()
window.mainloop()
Approach3 using style:
import tkinter as tk
from tkinter import ttk
def change_button_style(event):
widget = event.widget
if widget['style'] == 'TButton':
widget.configure(style='VanishedText.TButton')
else:
event.widget.config(style='TButton')
BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'
window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')
button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()
style.map('VanishedText.TButton',
foreground =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
background =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
focuscolor=[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)])
window.mainloop()
You Can Simply Config And Set Text :
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()

Open window with image using button in Python

I want to open a new window which includes an image. I don't know how to achieve this. Currently, my code opens up a plain window with a button saying show image.
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.configure(background = "black")
obrazek = ImageTk.PhotoImage(Image.open("dn.jpg"))
def click():
MyLabel = Label(root, image = obrazek)
Tk()
MyLabel.pack()
myButton = Button(root, text = "Wyjście", command = click, fg = "white", bg = "#000000")
myButton.pack()
mainloop()
This is minimal working example.
You have to use Tk() only to create main window. For other windows you have to use Toplevel. And when you have it in variable ie. top then you have to use it as first argument in Label (or other widget) to display widget in this window.
BTW: PEP 8 -- Style Guide for Python Code
#from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
from PIL import ImageTk, Image
# --- functions ---
def click():
top = tk.Toplevel()
my_label = tk.Label(top, image=my_image)
my_label.pack()
# --- main ---
root = tk.Tk()
#root.configure(background="black") # PEP8: without spaces around `=`
my_image = ImageTk.PhotoImage(Image.open("lenna.png")) # PEP8: english names
my_button = tk.Button(root, text="Wyjście", command=click) # PEP8: lower_case_names for variables
my_button.pack()
root.mainloop()
Image from Wikipedia: Lenna
EDIT:
I hope you didn't create PhotoImage inside function because there is bug in PhotoImage which removes image from memory when it is assigned to local variable in function. And then you can see empty image. It has to be assigned to global variable or to some object - ie. my_label.img = image

How to display Url Image Tkinter python 3

How can I visualize an url of an image with tkinter ("http://images.amazon.com/images/P/0374157065.01.LZZZZZZZ.jpg").I would like to place it in the last column of the program, in standard size, I tried many guides but none of them ever worked. Thanks very mutch
Why not bind a URL to a Label like this?
from tkinter import *
import webbrowser
def callback(event):
webbrowser.open_new(r"http://www.your-image.com")
root = Tk()
link = Label(root, text="The image", fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", callback)
root.mainloop()

Python tkinter grid manager?

I just learned how to use tkinter in Python (3.2.2), and I'm having some problem using the grid manager. When I put button.grid(sticky=SE), for example, the button is not being put in the bottom-right and is just being put in the upper-left, ignoring the sticky value. What am I doing wrong here? I tried to search it but I couldn't really find out what I am doing wrong.
You probably need to set a minimum size for the widget containing the button.
If you don't, the container widget may shrink to occupy only the space required to display the button. If so, the sticky option will be meaningless since the container widget gives no space to show any difference.
For example, using a tk.Frame as the container widget:
import Tkinter as tk
class SimpleApp(object):
def __init__(self, master, **kwargs):
title = kwargs.pop('title')
frame = tk.Frame(master, borderwidth=5, bg = 'cyan', **kwargs)
frame.grid()
button = tk.Button(frame, text = title)
button.grid(sticky = tk.SE)
frame.rowconfigure('all', minsize = 200)
frame.columnconfigure('all', minsize = 200)
def basic():
root = tk.Tk()
app = SimpleApp(root, title = 'Hello, world')
root.mainloop()
basic()
yields
PS. I don't have tkinter installed in Python3.2 so I can't test this, but I think the only change you need to make this work with Python3.2 is
import tkinter as tk
instead of
import Tkinter as tk
When you say "What am I doing wrong here", you need to post your code, otherwise how would anyone be able to guess what's wrong.
The following works fine, placing the button in the lower right corner (SE) of the grid cell - the default is center, not upper left (NW).
from tkinter import Button, Label, Entry, Tk, SE
root = Tk()
Label(text="Lots o' Stuff", width=30, height=15,
borderwidth=2, relief="raised").grid(rowspan=2)
Entry().grid(row=0, column=2)
Button(text="Hit Me").grid(row=1, column=2, sticky=SE)
root.mainloop()

Categories

Resources