Hi I am a novice programmer.I am trying to use tkinter in pycharm....
When i try to use root or any other function it doesn't show any suggestion and gives not defined error.The program works fine in idle. But cann't seem to get it work with pycharm.Please help.I have already installed tkinter package and its also enabled in project interpreter settings....
here's what I am trying to do...
from tkinter import *
root = Tk()
topframe = Frame(root)
topframe.pack()
bottomframe = Frame(root)
bottomframe.pack(side = BOTTOM)
button1 = Button(topframe, text='Button 1', fg='red')
button2 = Button(topframe, text='Button 2', fg='blue')
button3 = Button(bottomframe, text='Button 3', fg='green')
button1.pack(side = LEFT)
button2.pack(side = LEFT)
button3.pack()
root.mainloop()
Just try this
from tkinter import Tk
root= Tk()
I also faced this problem before. This worked for me. I don't know the reason.
Note Tkinter has been renamed to tkinter in Python 3
tkinter — Python interface to Tcl/Tk.(Tk itself is not part of Python; it is maintained at ActiveState)
source : official Doc
Also there are certain classes that don't get imported when you try to import with *. When working with tkinter.
turn Around:
Solution 1.
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
root = tk.Tk()
Solution 2:check correct version is installed in pycharm.
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
root.mainloop()
Solution 3: Absolute Import
from tkinter import Tk
root= Tk()
w = Label(root, text="Hello, world!")
I had the same problem. I found that when I typed from tkinter there were 2 different options; you need to choose the option with a file icon next to it
Related
import tkinter as tk
from subprocess import check_call
def copy_name():
cmd = 'echo ' + name.strip() + '|clip'
return check_call(cmd, shell=True)
root = tk.Toplevel(background="black")
root.title("Copying")
root.resizable(False, False)
T = tk.Label(root, text=name, height=2, width=len(name) + 25, background="black", foreground="white")
T.pack()
button = tk.Button(root, text="Copy", command=copy_name, background="black", foreground="white")
button.pack()
tk.mainloop()
This is my code.
I just wanted to test this way of copying text...
About my expectations... i want to understand from where those windows are appearing, and how to stop it.
Im just a newbie in Python and Tkinter... so please, tell me what i did wrong
You are not using Toplevel(). You just wanted single window. Just replaced Toplevel to tk()
Code:
import tkinter as tk
from subprocess import check_call
def copy_name():
cmd = 'echo ' + name.strip() + '|clip'
return check_call(cmd, shell=True)
root = tk.Tk()
root.title("Copying")
root.resizable(False, False)
T = tk.Label(root, text='name', height=2, width=25, background="black", foreground="white")
T.pack()
button = tk.Button(root, text="Copy", command=copy_name, background="black", foreground="white")
button.pack()
tk.mainloop()
Screenshot:
Every tkinter window requires a root window - an instance of Tk. If you don't create one, one will be created automatically. When you do root = tk.Toplevel(background="black"), tkinter will first create an instance of Tk and then it will create your Toplevel, resulting in two windows.
The solution in this case is to call Tk instead of Toplevel. Also, you'll need to remove the background="black" argument and instead configure the background in a separate step.
root = tk.Tk()
root.configure(background="black")
As #Bryan said, you should forget about Toplevel(). The normal way is Tk().
Try this:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
label = tk.Label( root, text='Select:', background='green').pack(side='top')
btn1 = ttk.Button( root, text='Discard').pack()
btn2 = ttk.Button( root, text='Quit').pack()
while True:
root.mainloop()
And you should get:
I'm currently learning Tkinter and cannot find a solution to my problem. The Tkinter Label is not showing up in the window and no one has the solution why. I am on MacOS M1 Pro.
from tkinter import *
root = Tk()
# Create label widget
myLabel = Label(root, text="Hello World!")
# Pack it onto the screen
myLabel.pack()
root.mainloop()
Shown Result:
Try adding size to your root window.
from tkinter import *
root = Tk()
root.geometry("500x500")
# Create label widget
myLabel = Label(root, text="Hello World!")
# Pack it onto the screen.
myLabel.pack()
root.mainloop()
I am trying to display a tkinter window (with an image,in my case) that doesn't show on the taskbar. I've already tried using the method described here, but the person that answered didn't provide an example so I am unable to replicate it (and I'm using Windows 10 so maybe that is another factor to consider).
My code is here. I am using Python 3.5
from tkinter import Toplevel, Tk, Label, PhotoImage
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100")
window.overrideredirect(1)
photo = PhotoImage(file="testfile.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
The linked question contained an interesting comment recommending to reverse what was done in that other answer. Along with this accepted answer that explains that what is needed is just to set the WS_EX_TOOLWINDOW style, a simple example is:
import tkinter as tk
import tkinter.ttk as ttk
from ctypes import windll
GWL_EXSTYLE=-20
WS_EX_TOOLWINDOW=0x00000080
def set_toolwindow(root):
hwnd = windll.user32.GetParent(root.winfo_id())
style = windll.user32.GetWindowLongPtrW(hwnd, GWL_EXSTYLE)
style = style | WS_EX_TOOLWINDOW
res = windll.user32.SetWindowLongPtrW(hwnd, GWL_EXSTYLE, style)
# re-assert the new window style
root.wm_withdraw()
root.after(10, lambda: root.wm_deiconify())
def main():
root = tk.Tk()
root.wm_title("AppWindow Test")
button = ttk.Button(root, text='Exit', command=lambda: root.destroy())
button.place(x=10,y=10)
#root.overrideredirect(True)
root.after(10, lambda: set_toolwindow(root))
root.mainloop()
if __name__ == '__main__':
main()
Assumption: I'm using Python 3.6 and I'm working on Windows 10
Is possible to create a GUI with tkinter in which dragging a file in the window it returns the path of the file?
If with tkinter it's not possible, is there another solution that can solve the problem without installing additional libraries?
You need to install tkinterdnd2
pip install tkinterdnd2
code:
from tkinter import TOP, Entry, Label, StringVar
from tkinterdnd2 import *
def get_path(event):
pathLabel.configure(text = event.data)
root = TkinterDnD.Tk()
root.geometry("350x100")
root.title("Get file path")
nameVar = StringVar()
entryWidget = Entry(root)
entryWidget.pack(side=TOP, padx=5, pady=5)
pathLabel = Label(root, text="Drag and drop file in the entry box")
pathLabel.pack(side=TOP)
entryWidget.drop_target_register(DND_ALL)
entryWidget.dnd_bind("<<Drop>>", get_path)
root.mainloop()
I'm trying to make a basic window with the text "t" inside using Tkinter, however when running the code the shell spits out "NameError: name 'Label' is not defined". I'm running Python 3.5.2.
I followed the tutorials but the problem is in the label = Label(root, text="test") line.
import tkinter
root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)
label = Label(root, text="test")
label1.pack()
root = mainloop()
Is the label function different in 3.5.2?
You never imported the Label class. Try tkinter.Label
Check the import statements for those tutorials
Maybe they imply from tkinter import *
import tkinter
root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)
label = tkinter.Label(root, text="test")
label1.pack()
root = tkinter.mainloop() # <- prob need to fix this as well.
Because you didn't do from tkinter import * you need to invoke the Label from the tkinter module.
Alternatively you can do:
from tkinter import *
...
label = Label(root, text="test")
stumbled across the same Problem. Most beginners guides seem to mess up here.
I had to use a second line in the configuration:
!/usr/bin/python3
import tkinter
from tkinter import *
...
On Windows Operating System your code should run well but on macOS, you will get a problem. I don't know why something like that happen. Anyway try:
import tkinter,
from tkinter import*
And run
After that just write:
from tkinter import *
or
import tkinter
(not both this time)
It's a typo error...
Nothing to do with the import statements.
Label = with a capital L not l
Label(root, text="Username").place(x=20,y=20)
capitalize l