Tkinter button not clicking until I switch windows - python

I am following a Tkinter tutorial and I have a very basic GUI built. When I run it, the GUI opens and everything is fine, except the button is not immediately clickable. The only way I can get it to click is if I switch to a full screen window and back to the GUI. Below is the code I am using, along with a screenshot. Does anyone know why this is happening? If it does not make enough sense I can upload a video. Thank you.
import tkinter as tk
from tkinter import Frame
HEIGHT = 700
WIDTH = 800
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
frame = tk.Frame(root, bg='#ff9a7f')
frame.place(relwidth=2, relheight=1)
button = tk.Button(root, text='Test Button')
button.pack()
root.mainloop()

Related

Tkinter button appearing when used in a function

Working on a small project and I'm noticing my Tkinter button refuses to work in a def function. def upload_win(): comes up but I'm not able to get the button to appear on the ext1 = Tk() window. I'm new to python but I feel like this should work. I'm using pycharm if that helps at all.
from tkinter import *
root = Tk()
root.geometry('700x400+100+100')
root.maxsize(700, 400)
root.minsize(700, 400)
root.title("root")
def upload_win():
root.destroy()
ext1 = Tk()
ext1.geometry('700x400+100+100')
ext1.maxsize(700, 400)
ext1.minsize(700, 400)
ext1.title("1")
btn1_ext1 = Button(ext1, test="back")
btn1_ext1.place(x=590, y=360, height=30, width=100)
btn1_ext1.configure(bg="DodgerBlue3", fg="black")
btn1_root = Button(root, text="ext1", command=upload_win)
btn1_root.place(x=590, y= 360, height=30, width=100)
btn1_root.configure(bg="DodgerBlue3", fg="black")
root.mainloop()
I expect a window to appear and have a button. When the button is pressed the first window gets deleted and a new one appears with a back button attached. So far the back button has yet to be attached to anything but I can't get it to show up.

How do I make my tkinter image pop not show the tkinter option window

I have a simple code that opens an exe. This exe makes a pop-up image using Tkinter but there is an option to delete or minimise the photo. How do I get rid of this option?
here is my code
and here is the TK option I was talking about
[![enter image description here]
and here is the TK option I was talking about
You can try:
CODE_1: where it will show minize, maximize and exit but it will not work
from tkinter import *
window = Tk()
window.title("Grand Canyon")
canvas = Canvas(window, width=500, height=500)
canvas.pack()
#window.overrideredirect(1)
window.attributes('-disabled', True)
window.resizable(0,0)
window.mainloop()
CODE_2: where it will NOT show minize, maximize and exit because it will pack into the top of your window
from tkinter import *
window = Tk()
window.title("Grand Canyon")
canvas = Canvas(window, width=500, height=500)
canvas.pack()
window.overrideredirect(1)
#window.attributes('-disabled', True)
window.resizable(0,0)
window.mainloop()

Code not printing the events in tkinter python

So I'm trying to make a code that prints the events/actions that happens on the tkinter area. When I run the script I don't get an error but when I'm clicking on the graphics area nothing prints out.
import tkinter
canvas = tkinter.Canvas(width=640, height=480)
canvas.pack()
def function1(event):
print(repr(event))
canvas.bind("ButtonPress-1", function1)
canvas.mainloop()
You need to define the instance of tkinter.Tk() and use it as root. The following implementation works for me as expected:
import tkinter
root = tkinter.Tk()
def function1(event):
print(repr(event))
canvas = tkinter.Canvas(root, width=640, height=480)
canvas.bind("<ButtonPress-1>", function1)
canvas.pack()
root.mainloop()

Python Tkinter : Canvas with Image takes forever to execute

I've been experimenting a little with the Canvas function of Tkinter and tried to add some imagefile to the window, to test if i can create proper background images.The code so far seems to be working fine (atleast i get no errors when executing). But as soon as i try to execute the Python script it just takes forever to load and doesn't display a window or anything. As soon as i remove the Canvas block from my script it works fine.
from Tkinter import *
root = Tk()
root.title("ImageTest")
root.geometry("350x150")
root.minsize(350,150)
root.maxsize(350,150)
#***** Canvas *****
photo = PhotoImage(file="derp.gif")
w = Canvas(root, width=350, height=150)
w.pack()
w.create_image(0,0, anchor=NW, image=photo)
w.image = photo
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