After seeing this, PhotoImage not showing up the image associated with it I wondered why omitting the type of option does not throws an error instead of just not showing up the image?
This code does not show the image without throwing any error
import tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage("ban.gif")
tk.Label(root,image=image1).pack()
tk.Label(root, text="some string here").pack()
root.mainloop()
But this one works fine
import tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage(file="ban.gif")
tk.Label(root,image=image1).pack()
tk.Label(root, text="some string here").pack()
root.mainloop()
On effbot it doesn't say anything about it so I checked tcl man page for creating photos but still can not find why it behaves like this.
Also, if those two are duplicate ones, let me know so I will delete/close vote this one.
When you specify a function with named arguments in python, those named arguments appear in a specific order. If you do not supply a name when defining these arguments, they are applied in the order that the arguments appear in the function definition.
In the case of PhotoImage, the first keyword argument is for the name of the image, not a path to a file. So, PhotoImage("ban.gif") is the same as doing PhotoImage(name="ban.gif"). It doesn't throw an error because "ban.gif" is a valid name, and there are use cases where you want to create an image without referencing a file.
Related
I've come across two different ways to create a menu bar:
import tkinter as tk
window = tk.Tk()
menu_bar = tk.Menu() ■
window.config(menu = menu_bar) #2
window.mainloop()
import tkinter as tk
from tkinter import Menu
window = tk.Tk()
menu_bar = Menu(window) ■
window.configure(menu = menu_bar)
window.mainloop()
Question: what's the difference between these lines of code? By this I mean, why is the syntax different If they do the same? (I've marked the referred lines of code as ■). How importing Menu from tkinter affect the lines of code?
What's the difference between these lines of code?
It does the same thing(except the imports. With from tkinter import Menu, you are specifically just importing Menu and nothing else from tkinter. But in the first example, you are importing whole tkinter and you can refer to tkinter.Menu as tk.Menu. But in the second example, you just have to say Menu.
Note that in the second example you can still use tk.Menu as well as Menu. So the second import is rendered useless, and can be removed. It is better to follow the first example.
As mentioned by AST, if you say Menu(), an existing instance of Tk() will be passed as the master argument implicitly. But if you say Menu(win), you are passing win explicitly. It is always recommended to pass the parent argument explicitly while you work with multiple windows so as to not cause confusions.
I had started writing a tkinter program when I stumbled across this problem in my code:
elif (xtimes=="" or xtimes=="Optional") and (t!="" or t!="Optional"):
amount
years=0
while years<t:
principle=principle+((interest/100)*(principle))
years+=1
amount=principle
finallabel=Label(root,text="Your Final Amount will be",amount,"at the end of",years,"years")
finallabel.grid(row=13,column=0)
Under the elif statement, I have calculated amount and I want to show the answer using a label, which gives the error: "positional argument follows keyword argument"
I think that I want to send the variable amount through the text, like in normal python, but the code makes it think like I am passing some parameter called amount which doesn't exist.
Please help.
the only positional argument you need to pass is Label(root).
So if you do Label(text='my text', root) it gives this error.
this works:
import tkinter as tk
root = tk.Tk()
lab = tk.Label(root, text='hi')
lab.pack()
root.mainloop()
this not:
import tkinter as tk
root = tk.Tk()
lab = tk.Label(text='hi',root)
lab.pack()
root.mainloop()
After update.. Let's look on this line of your code here:
finallabel=Label(root,text="Your Final Amount will be",amount,"at the end of",years,"years")
What you did here, was to parse arguments through the interface of the Label class to make an instance of it, with the config of given arguments.
the tkinter Label class knows the arguments that can be found here.
So comparing your Label with the available parameters, you will notice that amount and years arent part of them. The only Posistional argument that the Label class of tkinter is expecting is the master, followed by keyword arguments **options. Read this.
What you trying to do is a string with variables and there are several ways to achieve this. My personal favorite is the f'string.
And with a f'string your code will be look like this:
finallabel=Label(root,text=f'Your Final Amount will be {amount},at the end of {years} years')
Let me know if something isnt clear.
Every source of documentation I've been able to find for this say that by calling itemconfig without any arguments will return a list of possible attributes that can be edited. I tried to do this with the following code:
import tkinter
from tkinter import *
master = Tk()
w = Canvas(master, width=200, height=100)
w.pack()
print(w.itemconfig('bg'))
...And it only returned an empty dictionary (it throws an error when I put in zero arguments, and no other arguments I could think of have given me results, except for actually using the function as intented)
I find it rather silly that nobody has written down all of the attributes that itemconfig can use (or if they have, I can't find it); being able to actually retrieve attributes would be nice, but far more than that, I'd like a complete list to be written down, since I can't find it anywhere.
Why is my Tkinter image not working? It is in the same directory, all commands are right but I get the error:
_tkinter.TclError: image "pyimage1" doesn't exist.
What's wrong?
fertig=tkinter.Tk()
fertig.title("Window")
text=tkinter.Label(fertig,text="Success")
text.pack()
w = tkinter.PhotoImage(file="/Users/Hannes/Desktop/Spambot/successful.gif ")
w = tkinter.label(fertig,image=w)
w.pack()
knapp=tkinter.Button(fertig,text="Ok",command=lambda:close())
knapp.pack()
knapp.mainloop()
The following works for me on my Windows system. I had to fix and add a few (unrelated) things to get the code in your question to work, but after doing so I found the real reason the image doesn't display.
So, the error that relates most directly to the that problem is because you're overwriting the variable w: After you assign a tkinter.PhotoImage() value to it, you immediately assign a another value (the tkinter.Label) to using its current value (image=w). The second assignment causes the tkinter.PhotoImage() object that was in it to be lost. Since there are no more references to to, it will be garbage-collected at some point.
To fix that, I simply assign the PhotoImage object to a separate variable img.
Note, too, that (apparently) having the trailing space character in the filename isn't a problem (at least not on Windows).
Here's some "official" documentation specifically about the PhotoImage class that discusses the need for keeping a reference around to the original—see the NOTE: at the end—when using it with other tkinter widgets (like a Label).
import tkinter
def close(): # just a placeholder implementation.
print('close() called')
fertig=tkinter.Tk()
fertig.title("Window")
text=tkinter.Label(fertig, text="Success")
text.pack()
#w = tkinter.PhotoImage(file="/Users/Hannes/Desktop/Spambot/successful.gif ")
img = tkinter.PhotoImage(file=r"C:\vols\Files\PythonLib\Stack Overflow\successful.gif ")
w = tkinter.Label(fertig, image=img)
w.pack()
knapp=tkinter.Button(fertig, text="Ok", command=lambda: close())
knapp.pack()
knapp.mainloop()
Here's what it looks like (using an image of my own).
so I'm starting to learn python and I need to write a script that edits a CSV file. I found this online and had a few questions about what it's exactly doing since the original programmer didn't explain. My question right now though is about syntax. I'm a little confused about some of these lines:
import Tkinter,tkFileDialog
root = Tkinter.Tk()
root.filename = tkFileDialog.askopenfilename(initialdir = "/", title =
"Select a file", filetypes = (("csv files", "*.csv"),))
So my first question is what root equals. I understand I imported two modules called Tkinter and tkFileDialog(Correct me if I'm wrong) into my file. I then created a variable called root and set it equal to a method call?
root = Tkinter.tk()
Next, what does this line do? Is filename a method in one of those modules? I read something about widgets...are widgets methods? As in the word is used interchangeably?
root.filename
Thank you in advance!
You may benefit more form some youtube tutorials on python methods/functions and classes but I can answer your questions in general terms.
So my first question is what root equals.
root is the variable name assigned to the instance that is being created with tkinter.Tk()
This allows you to interact with that instance of tkinter and you can use root to place widgets on the main window of the GUI.
Next, what does this line do? root.filename
root.filename is only a variable name. tkFileDialog.askopenfilename is the class method being used to get the file and assign the file information to the variable name root.filename
So what you are doing here is importing the library tkinter that contains many class methods that can be used to build and manipulate a GUI interface.
note that for an instance of tkinter you will need a mainloop() at the end of your code to make it work. So at the end of your code you will need to have something like root.mainloop() to make sure the program will work as long as everything else is done correctly.