Using tkinter with ibPy - python

I am trying to use tkinter with ibPy. I am using Spyder (Spyder 2.3.0). When I enter the sample program
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
I receive the message:
File "/Users/Ameade/Tkinter.py", line 8, in <module>
from tkinter import *
ImportError: No module named tkinter
Do you know where I can get this module? I am working on a Mac (OSX 10.9.4).

It seems you named your sample program file Tkinter.py. You should change this name to something else and it should work.
EDIT
As Kevin said, give to the file any other name but not the name of a python module (the extension must remain .py), so you could name it my_amazing_program.py. And keep the content of the file the same as you originally posted it - if you're using python 3+:
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
If you're using python 2+ change tkinter to Tkinter on the import line:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

You may need to check the version of python you are using.
import tkinter works on python 3(instead of Tkinter)

Related

I have a weird problem with tkinter when using python

I am trying to learn how to use Tkinter, but whenever I want to execute my code I always get this problem: (NameError: name 'label' is not defined) or (NameError: name 'button' is not defined).
As a beginner, it seems to me that the problem is with my code editor. (BTW I am using VScode)
this is my code:
from tkinter import *
root = Tk()
mylabel = label(root, text='Hello World')
mylabel.pack()
root.mainloop()
And as I said, this also happens with this one:
from tkinter import *
root = Tk()
mybutton = button(root, text='Hello World')
mybutton.pack()
root.mainloop()
you have caps error its Button and Label not button and label

Python('module' object is not callable )Tkinter

I'm getting this issue as mentioned in the title.
I'm trying to run a file to print hello world in the widget. I'm getting what I want when I run it in my system, but when I'm running it in colab its not working.
Code:
import tkinter
root = tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-db74150bd164> in <module>()
1 import tkinter
2
----> 3 root = tk()
4
5 myLabel = Label(root, text="Hello World!")
TypeError: 'module' object is not callable
I tried changing tk into various forms(Tk, tK, Tkinter, tKinter), but it isn't working anyhow.
When you see Tk(), it is an instance of Tk() class present in __init__.py file in tkinter folder.
Since you have imported tkinter, you have to specify tkinter.Tk()to create a instance of Tk()
import tkinter
root = tkinter.tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
In some programs, you can also see tk.Tk(). This is because the module tkinter is imported as tk:
import tkinter as tk
See the source code of tkinter
At line 2273 in __init__.py, you can see:
class Tk(Misc, Wm):
"""Toplevel widget of Tk which represents mostly the main window
of an application. It has an associated Tcl interpreter."""
_w = '.'
def __init__(self, screenName=None, baseName=None, className='Tk',
useTk=True, sync=False, use=None):
...
There are some errors:
import tkinter as tk
root = tk.Tk() # tk() you can't call a module, write tk.Tk() instead.
myLabel = tk.Label(root, text="Hello World!") # add tk.
myLabel.pack()
root.mainloop()
you can just import tkinter as
from tkinter import *
by using this it will work

Sounds in Python (tkinter) [duplicate]

I am building a program for Windows PCs that contains a lot of buttons and seems very plain. So I was wondering, can I make it so when you push a button (using tkinter), can I play a sound to liven up the program a bit? Please keep in mind I am learning so please dumb it down a bit.
Assuming your file is a WAV:
from tkinter import *
from winsound import *
root = Tk() # create tkinter window
play = lambda: PlaySound('Sound.wav', SND_FILENAME)
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
Assuming your file is a MP3:
from Tkinter import *
import mp3play
root = Tk() # create tkinter window
f = mp3play.load('Sound.mp3'); play = lambda: f.play()
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
You might want to consider using pygame as a cross-platform alternative to winsound.
import tkinter as tk
from pygame import mixer
mixer.init()
sound = mixer.Sound("sound.ogg")
root = tk.Tk()
tk.Button(root, command=sound.play).pack()
root.mainloop()
Refer to the docs for more information.
You first need to link the click of your mouse on the image, with an even handler, then simply define an on_click function:
def on_click(event):
winsound.Beep('frequency', 'duration')
Here you can find more information about playing sounds in python.
Just use
import os
os.system("play sound.mp3")

using tkinter module with python 3.6

I'm trying to write simple things in a Tkinter module using python 3.6 in Anaconda. This is my code
from tkinter import *
root = Tk()
thelabel = label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
but I get the error:
TclError: can't invoke "label" command: application has been destroyed
ERROR: execution aborted
and I keep getting a blank Tkinter window whenever I run my code and I don't understand what is the problem, thanks:)
You have a small error...
Here is the correct way:
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
You should try reading a bit on tkinter.
Here are some references specifically on label.
Hope you find this helpful!
change your code to this
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
Labels start with a capital L not small l it will fix your error

How to set default text for a Tkinter Entry widget

How do I set the default text for a Tkinter Entry widget in the constructor? I checked the documentation, but I do not see a something like a "string=" option to set in the constructor?
There is a similar answer out there for using tables and lists, but this is for a simple Entry widget.
Use Entry.insert. For example:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
e = Entry(root)
e.insert(END, 'default text')
e.pack()
root.mainloop()
Or use textvariable option:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
v = StringVar(root, value='default text')
e = Entry(root, textvariable=v)
e.pack()
root.mainloop()
For me,
Entry.insert(END, 'your text')
didn't worked.
I used Entry.insert(-1, 'your text').
Thanks.

Categories