Import tkinter as tk does not work with text widget - python-3.x

I tried to create a text widget with option import tkinter as tk but I don't know why the text methods not working with my object.
If I use from tkinter import * then it is all good, but as I read this is not the recommended importing method.
So, could you please advise why first code works and the second doesn't? What am I missing?
This works:
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()
root.mainloop()
This doesn't:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()
root.mainloop()
Thanks

if you are using:
import Tkinter as tk
INSERT is a constant defined in Tkinter, so you also need to precede it with Tkinter.
you need to use INSERT like:
tk.INSERT
your code:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.insert(tk.INSERT, "Hello.....")
text.insert(tk.END, "Bye Bye.....")
text.pack()
root.mainloop()
in this case if you are using :
text.insert(INSERT, "Hello.....")
you will get an error:
NameError: name 'INSERT' is not defined

Related

What should I do to get the first example of ttk.style working properly?

I wanted to try the ttk.Style() settings, so I copied the example from the docs at Ttk Styling and ran it. I noticed that no matter what I set the "raised" parameter to, it looks the same. Here's the code I ran:
from tkinter import ttk
import tkinter
root = tkinter.Tk()
Running Python 3.10.8
ttk.Style().configure("TButton", padding=10, relief="raised",
background="#fff")
btn = ttk.Button(text="Sample")
btn.pack()
root.mainloop()
Running this, the button looks like this:
If I change "raised" to "flat" or "sunken", it looks the same. What should I do to see the button style change?
The button doesn't know you want it to use the style defined above. Try adding style=[style_name] to to the btn definition. Here is an example below.
from tkinter import ttk
import tkinter
root = tkinter.Tk()
style=ttk.Style()
style.configure("TButton", padding=10, relief="raised",
background="#fff")
btn = ttk.Button(text="Sample", style= "TButton")
btn.pack()
root.mainloop()
Hope it helps.

unable to apply a ttk theme to .toplevel()

I'm trying to rcreate a slasph screen which doubles as an about screen on a menu bar in tkinter and python 3.
so far I've read I should be using a Toplevel() method on the second splash_root but It justs send me the error that, name 'Toplevel' is not defined.
Have I missed something basic here or just misunderstood how Toplevel() works?
import tkinter as tk
import tkinter.ttk as ttk
from PIL import ImageTk, Image
from ttkthemes import ThemedTk
def splash_screen():
splash_root = Toplevel(root)
splash_root.geometry("500x400")
img_title = ImageTk.PhotoImage(Image.open("art/icons/random.png"))
lbl_img_title = ttk.Label(splash_root,image=img_title)
lbl_img_title.pack(side="top",pady=20)
splash_label_by = ttk.Label(splash_root, text="name")
splash_button = ttk.Button(splash_root, text="close", command=lambda: splash_root.destroy())
splash_label_by.pack(pady=20, padx=20)
splash_button.pack(ipadx=10, ipady=10)
splash_screen()
root = ThemedTk(themebg=True)
root.set_theme('black')
root.title("splash")
root.geometry("500x500")
root.mainloop()
The answer to your question:
#Toplevel(root) is not defined
tk.Toplevel(root) #will solve your Toplevel problem.
If you call
splash_screen()
before declaring root, root will be not defined and your code still wont work.

Unexpected Blank Window Tkinter

A blank window along with the main window is created.
I've seen other questions but my case is different I'm not using any constructor.
The blank window appears when I initialize ttk.style.
correct_style = ttk.Style()
correct_style.configure('correct.TButton',background='#39b54a')
root = ThemedTk(theme="equilux")
If I delete these lines, the empty window doesn't appear.
I think you call the two lines before creating the instance of Tk(), something like below:
import tkinter as tk
from tkinter import ttk
correct_style = ttk.Style()
correct_style.configure('correct.TButton',background='#39b54a')
root = tk.Tk()
...
root.mainloop()
As ttk.Style() requires an instance of Tk(), if there is none, it will be created implicitly for you. So there will be two instances of Tk().
Move the two lines after creating Tk():
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
correct_style = ttk.Style()
correct_style.configure('correct.TButton',background='#39b54a')
...
root.mainloop()

How to deal with "AttributeError: '_tkinter.tkapp' object has no attribute 'messagebox'"?

I'm studying about 'tkinter' recently.
Pycharm told me here is no 'messagebox', but 'messagebox.py' does exist in my 'tkinter'.
Many ways I gained from Internet can't solve this.
Please help me, I'll appreciate for that.
greet_button = Button(
table,
text = 'Yes',
width = 20,
height = 2,
bg = 'yellow',
fg = 'red',
command = table.messagebox.showinfo(title='Hello', message='Hello!')
)
Some packages of tkinter need a extra import like ttk, ttkthemes or messagebox.
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
def err():
tk.messagebox.showinfo(title='hello', message='hello')
erro = tk.Button(root, text="Top", command=err)
erro.pack()
root.mainloop()
As oneliner you would need an annonymus function:
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
erro = tk.Button(root, text="Top", command=lambda:tk.messagebox.showinfo(title='hello', message='hello'))
erro.pack()
root.mainloop()

Tkinter Label Text Not Appearing

I use this code (from https://www.python-course.eu/tkinter_labels.php) but no text appears in the label.
import tkinter as tk
root = tk.Tk()
w = tk.Label(root, text="Hello Tkinter!")
w.pack()
root.mainloop()
I suspect that any problem may involve the mainloop function.

Resources