Unexpected Blank Window Tkinter - python-3.x

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()

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.

Import tkinter as tk does not work with text widget

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

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.

How to run a python tkinter app without appearing in unity panel or alt-tab

I am working on a countdown timer for Ubuntu using Python and tkinter.
I have created most of the parts and now I want my app to be able run without appearing in Unity panel or Alt-Tab switching sequence. Is there any way to do this?
And also I'd like to whether it is possible to create a moveable window without the title bar. I tried root.overrideredirect(1).
But with it I am unable to move the window.
Here's the code for my program.
#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import TOP,LEFT
import time
import datetime
import sys
class Countdown(ttk.Frame):
def __init__(self,parent=None, endDate=None):
ttk.Frame.__init__(self,parent)
self.style = ttk.Style()
self.style.theme_use("clam")
self.pack()
endDate = endDate.split("/")
self.endTime = datetime.datetime(int(endDate[2]),int(endDate[1]),int(endDate[0]))
self.setWidgets()
self.initWidgets()
def setWidgets(self):
self.dLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
self.dLbl.pack(padx=10,pady=10,side=LEFT)
self.hLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
self.hLbl.pack(padx=10,pady=10,side=LEFT)
self.mLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
self.mLbl.pack(padx=10,pady=10,side=LEFT)
def getTimeDelta(self):
self.curDate = datetime.datetime.now()
self.diff = self.endTime - self.curDate
self.tSec = self.diff.total_seconds()
self.days = self.diff.days
h = int(((self.tSec) - self.days*24*60*60)/3600)
self.hours = h if h>0 else 0
m = int(((self.tSec) - (self.hours*60*60+self.days*24*60*60))/60)
self.minutes = m if m>0 else 0
self.sec = int(self.tSec - self.minutes*60)
return [self.days,self.hours,self.minutes+1]
def initWidgets(self):
def set():
dhm = self.getTimeDelta()
self.dLbl["text"]=str(dhm[0])+" Days"
self.hLbl["text"]=str(dhm[1])+" Hours"
self.mLbl["text"]=str(dhm[2])+" Mins"
self.after(1000,set)
set()
root = tk.Tk()
root.title(sys.argv[1])
app = Countdown(root, sys.argv[2])
app.mainloop()
To move a window without borders you can take a look at this question for a simple example of how to implement what you want and just keep building on top of it.
For the hiding, you could use .iconify(), theoretically minimizing the app to the tray thus hiding it, and .deiconify(), for example:
root = tk.Tk()
root.iconify()
ps. If it don't work on Ubuntu/Unity you may have to use other GUI framework with support for this behavior on Ubuntu, like GTK.
import tkinter
root = tkinter.Tk()
root.withdraw()
root.mainloop()
Will hide the window.
Make your own titlebar, like so:
import sys
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk() #create the window
titlebar = tk.Label(root,height=2, bg='cyan', fg='navyblue', text=sys.argv[0]) #create the titlebar
resizable = ttk.Sizegrip(root) #make the window resizable
titlebar.pack(fill='both') # display the titlebar
root.overrideredirect(1) #make the window run without appearing in alt tab
#root.withdraw()
#root.deiconify()
root.geometry('200x200') #set the window size to 200x200
resizable.pack(fill='y',side='right')
def ontitlebarclicked(event):
global lastposition
lastposition=[event.x,event.y]
def ontitlebardragged(event):
global lastposition
windowposition=[int(i) for i in root.geometry().split('+')[1:]] # get the window position
diffposition=[event.x-lastposition[0],event.y-lastposition[1]]
widthheight=root.geometry().split('+')[0]
root.geometry(widthheight+'+'+str(windowposition[0]+diffposition[0])+'+'+str(windowposition[1]+diffposition[1]))
titlebar.bind('<Button-1>',ontitlebarclicked)
titlebar.bind('<Button1-Motion>',ontitlebardragged)
titlebar.focus_force()

Resources