Python Tkinter delete title bar - python

I'm looking for very long time for a solution for this. I want to delete the Titlebar of a Tk window, like with the function "overridedirect()". My problem with that function is, that there is no icon on the taskbar of the OS.
I also tried it with "root.attributes("-fullscreen", 1)" and tried to scale it down, but that doesn't work either.
I hope somebody knows a good solution, thanks for help!
My code looks kind of like this now:
from tkinter import *
class Main(Frame):
def __init__(self, root):
**...**
#There are more classes after this one, but defined the same way
def main():
root = Tk()
root.geometry("800x400+0+0")
root.minsize(700, 400)
root.title("Title")
#root.overrideredirect(True)
#root.iconify()
##root.attributes('-topmost', 1)
##root.attributes("-fullscreen", 1)
##root.wm_state("zoomed")
if __name__ == "__main__":
main()

You cannot do what you want. You can either have no titlebar and no icon, or you can have a titlebar and icon. There is no way to get one without the other.

Related

How to make window look like a win10 widget. Python

I'm trying to create a windows app that shows weather like windows 10 widgets using Tkinter. The issue I ran into is that I cannot remove the window's border to make it look like a real widget.
Here's what it looks like and what I'd like to remove:
self.root.overrideredirect(1) kinda works but I still can minimize the window, which is what I do not want to be available.
That's what I want it to look and behave like:
To sum up:
How to disable window title and all of that on top properly?
How to move the window after, set the position of it on the screen?
I'm new to Python, any help is appreciated. Thank you all. Peace
To clarify: The app is supposed to be not affected by tab+alt (and other ways to minimize it) and be always on desktop, not on top of any other app, right on the desktop.
UPD
The thing I missed working around with win32gui is that I cannot get the handle the way I was trying to due to the fact that the code of getting the handle before the window itself was created (before mainloop) so root.wait_visibility() was the solution for me. After that I got the handle using EnumWindows and positioned my window the way I needed, finally disabling the border with root.overrideredirect. The suggested solutions will be tried as well. Thank you for help!
top-right corner of the desktop:
You can try this:
root.overrideredirect(1)
root.geometry('250x150+900+600')
#900+600 is x+y axis position of window
And for making close button like of widget you can make transparent frame in side, put button there and in command .destroy().
Edit
From TheLizzard's comment, visit here for further information. From there:
To make the window draggable, put bindings for (mouse clicks) and (mouse movements) on the window.
import tkinter
class Win(tkinter.Tk):
def __init__(self,master=None):
tkinter.Tk.__init__(self,master)
self.overrideredirect(True)
self._offsetx = 0
self._offsety = 0
self.bind('<Button-1>',self.clickwin)
self.bind('<B1-Motion>',self.dragwin)
def dragwin(self,event):
x = self.winfo_pointerx() - self._offsetx
y = self.winfo_pointery() - self._offsety
self.geometry('+{x}+{y}'.format(x=x,y=y))
def clickwin(self,event):
self._offsetx = event.x
self._offsety = event.y
win = Win()
win.mainloop()
You can use this for some part in top of the window rather then in whole window. By making new frame there implement it, to make your widget moveable.
And from second comment of TheLizzard, visit here for more information. From there:
If you want the window to stay above all other windows.
root.attributes("-topmost", True)

Tkinter new window declaration

Recently while using python tkinters declaration has not been working. What i mean by this is that a for any file where tkinter has not been imported before, simple code such as creating a window is not possible. Has anyone else encountered this issue? If so, how is it solved?
Any feedback is appreciated. Thanks.
The code is simply supposed to open a window in tkinter. But, when run, no window is displayed.
from tkinter import *
def sample():
window = Tk()
sample()
That's so interesting.
You have to use tkinter.Tk() instead of Tk().
Thanks
Try This :
from tkinter import * # Importing everything from the Tkinter module
win = Tk() # Initializing Tkinter and making a window
def sample():
new_win = Toplevel() # This creates a new window
sample()
win.mainloop() # If this is not there, the code won't work

Python Tkinter how to hide a widget without removing it

I know similar things have been asked a lot, but I've tried to figure this out for two hours now and I'm not getting anywhere. I want to have a button in a Tkinter window that is only visible on mouseover. So far I failed at making the button invisible in the first place (I'm familiar with events and stuff, that's not what this question is about) pack_forget() won't work, because I want the widget to stay in place. I'd like some way to do it like I indicated in the code below:
import tkinter as tki
class MyApp(object):
def __init__(self, root_win):
self.root_win = root_win
self.create_widgets()
def create_widgets(self):
self.frame1 = tki.Frame(self.root_win)
self.frame1.pack()
self.btn1 = tki.Button(self.frame1, text='I\'m a button')
self.btn1.pack()
self.btn1.visible=False #This doesnt't work
def main():
root_win = tki.Tk()
my_app = MyApp(root_win)
root_win.mainloop()
if __name__ == '__main__':
main()
Is there any way to set the visibility of widgets directly? If not, what other options are there?
Use grid as geometry manager and use:
self.btn1.grid_remove()
which will remember its place.
You can try using event to call function.
If "Enter" occurs for button then call a function that calls pack()
and if "Leave" occurs for button then call a function that calls pack_forget().
Check this link for event description:List of All Tkinter Events
If you wish your button to stay at a defined place then you can use place(x,y) instead of pack()

Python: How to use a tkinter Checkbutton in OOP functions

I have got stuck with this particular bit of code. I have condensed down the problem to this section.
I am running a sort of menu in Python, where the first menu sends you to the second menu, and in the second menu, there is a checkbutton the user can toggle on/off. In the third menu, I want it to read if the checkbutton is on/off and convert it to a Boolean. Code:
import tkinter as tk
class MainMenu(object):
def __init__(self):
self.launch_MainMenu()
def launch_MainMenu(self):
self.master = tk.Tk()
tk.Button(self.master,text="MY BUTTON",command= lambda:self.launch_SideMenu()).grid()
tk.mainloop()
def launch_SideMenu(self):
self.master2 = tk.Tk()
self.var1 = tk.IntVar()
tk.Checkbutton(self.master2,variable=self.var1).grid()
tk.Button(self.master2,text="Test",command= lambda:self.launch_FinalMenu()).grid()
def launch_FinalMenu(self):
d = bool(int(self.var1.get()))
print(d,self.var1.get())
mainMenu = MainMenu()
Output: Whether the checkbox is on or off, it outputs "False 0".
Any help would be very much appreciated!
As per the hint from Lafexlos, the error is in calling tk.Tk() twice. For a new window, you must use tk.Toplevel().
Simply changing the keyline to:
self.master2 = tk.Toplevel()
fixes everything. This took me a long time to work out. Thanks for the help, and best of luck to you coders reading this in the future.

Tkinter full screen

I used this snippet to attempt full screen in Windows, and this is what it gave me:
How can I fix this? It seems like full screen isn't supported at all or something with Tkinter. It's Windows 8, if that matters. It's also Python v2.7.2.
Try win.state('zoomed'), where win is your Tk window instance.
Edit :
Try something like this. Simply treat this class like a Tk window class.
class Void (tk.Tk) :
def __init__ (self, color='black') :
tk.Tk.__init__(self)
self.wm_state('zoomed')
self.config(bg=color)
self.overrideredirect(True)
self.attributes('-topmost', True)

Categories