I'm trying to write a code that prints a Frame to the screen with a Button and
Canvas in it
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
self.text =tk.Text(height=20,width=10)
self.text.pack()
self.canvas=tk.Canvas(fill='Black')
self.canvas.pack()
def on_button(self):
print(self.entry.get())
app = SampleApp()
app.mainloop()
As soon as I run it, I get an error:
_tkinter.TclError: unknown option "-fill"
I have no idea why.
Fill is a create_rectangle argument, not a constructor argument:
self.canvas.create_rectangle(0, 0, width, height, fill = "black")
The canvas doesn't use a fill option to define what the background is; the error is coming out of the lower levels of the Tkinter code where it flips to the underlying Tcl/Tk runtime; option names get a hyphen put in front of them, and the error otherwise means what it says, “don't know what fill is in this context” (paraphrased).
However, the canvas does use a background option that takes a colour. Try:
self.canvas=tk.Canvas(background='Black')
You can also create rectangles on the canvas; those are fillable. The overall canvas isn't a rectangle, it's a widget.
Related
I have been creating an application for taking the test. So, for that, I have to do two things.
First, disable the drag of the Tkinter window and don't let the user focus on other windows rather than my application window. This means I wanted to make my application such that, No other application can be used while my application is in use.
Try this:
import tkinter as tk
class FocusedWindow(tk.Tk):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Force it to be unminimisable
super().overrideredirect(True)
# Force it to always be on the top
super().attributes("-topmost", True)
# Even if the user unfoceses it, focus it
super().bind("<FocusOut>", lambda event: self.focus_force())
# Take over the whole screen
width = super().winfo_screenwidth()
height = super().winfo_screenheight()
super().geometry("%ix%i+0+0" % (width, height))
root = FocusedWindow()
# You can use it as if it is a normal `tk.Tk()`
button = tk.Button(root, text="Exit", command=root.destroy)
button.pack()
root.mainloop()
That removed the title bar but you can always create your own one by using tkinter.Labels and tkinter.Buttons. I tried making it work with the title bar but I can't refocus the window for some reason.
One way to do this is by the following, another could be to overwrite the .geometry() method of tkinter.
In the following code I simply had get the position by using winfo_rootx and winfo_rooty. After this you can force the window by calling the geometry method via binding the event every time the window is configured.
import tkinter as tk
def get_pos():
global x,y
x = root.winfo_rootx()
y = root.winfo_rooty()
def fix_pos():
root.bind('<Configure>', stay_at)
def stay_at(event):
root.geometry('+%s+%s' % (x,y))
root = tk.Tk()
button1 = tk.Button(root, text='get_pos', command=get_pos)
button2 = tk.Button(root, text='fix_pos', command=fix_pos)
button1.pack()
button2.pack()
root.mainloop()
I need to change the content of an entry whenever the tkinter frame is shown. Below is what I have so far, and it doesn't seem to work. I have tried to use data = self.read() and then now.insert(0, data) and that has not worked either. If the value is displayed then it doesn't get changed every time the class ReadLabel1 is called.
class ReadLabel1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="blue")
label = tk.Label(self, text="SomeData:", font = "Times 12", bg="blue")
label.place(x=10, y=100) #ack(pady=5,padx=30)
self.smStr = tk.StringVar()
now=tk.Entry(self, width=22, textvariable=self.read())
now.place(x=120, y=103)
def read(self):
# code to get data
return data
You need to turn 'change the content of an entry' into a one parameter callback, turn 'whenever the tkinter frame is shown' into an event, and then bind together the app, the event, and the callback. Here is a minimal example.
import time
import tkinter as tk
root = tk.Tk()
now = tk.StringVar()
lab = tk.Label(root, textvariable=now)
lab.pack()
def display_now(event):
now.set(time.ctime())
root.bind('<Visibility>', display_now)
root.bind('<FocusIn>', display_now)
Minimizing the window to a icon and bringing it back up triggers the Visibility event. Covering and merely uncovering with a different window did not, at least not with Windows. Clicking on the uncovered, or merely inactivated, window triggered FocusIn. You can experiment more with your system. I used this tkinter reference
I am new to Tkinter. I want to create a GUI that can support embedding audio files and that also has a background image. I have tried endlessly to install pygame to no avail. I cannot seem to figure out why it is not installing correctly so at this point I am just trying to find the easiest way possible to have these two options. Below is my attempt at displaying a background image using a canvas widget. However, I always get an error that my variables are not defined. I would really appreciate some feedback on what I am doing wrong, as well as any helpful tkinter tutorials that involve more than just the basics. Thanks in advance
from Tkinter import *
root = Tk()
root.geometry("500x500")
class Application(Frame):
def __init__(self, master):
#initialize the frame
Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.can = Canvas(root, width=160, height=160, bg='white')
self.pic = PhotoImage(file='speaker.gif')
self.item = can.create_image(80, 80, image=pic)
app = Application(root)
#kick off event loop
root.mainloop()
Every time you want to use an attribute of a class inside one of its methods, you need to prefix it with self.:
self.item = self.can.create_image(80, 80, image=self.pic)
# ^^^^^ ^^^^^
Otherwise, Python will treat the names as being local to the function and will raise an exception when it fails to find them.
Also, you forgot to call grid on your canvas widget:
self.can = Canvas(root, width=160, height=160, bg='white')
self.can.grid(...)
As for resources on Tkinter, you can check out these:
http://www.tkdocs.com/tutorial/onepage.html
http://effbot.org/tkinterbook/
I have a very simple program with TkInter in Python.
How to I use the "fill" or "expand" options with the following code?
from Tkinter import *
class Application(Frame):
def sayhello(self):
print "Hello!"
def createWidgets(self):
self.var = Button(self, text="Hello", command = self.sayhello)
self.var.pack(side=LEFT)
self.QUIT = Button(self, text="QUIT", fg="red", command = self.quit)
self.QUIT.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
Note: I have to use the self.var.pack() format so it can do a "command" when it's pressed...unless anyone has a better way?
To make an object fill it's container in the y axis, you would use the parameter fill="y" (or fill=Y if you import Y from Tkinter).
Note that this only controls how the widget fills its container. In your code, this makes the button fill the inner frame, but because your inner frame doesn't fill the main window in the y axis, you might not get the visual effect you expect.
Also, specifically in the case of buttons on the Macintosh, the button won't grow to fill the space. On OSX, buttons are native widgets which can't grow in height.
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()