How to create maximize minimize and close buttons in Tkinter (Python3)

Asked

Viewed 641 times

2

I would like to know how to create the maximize, minimize, and close buttons in Python (Tkinter). After the title bar is removed, new buttons must be created to customize the window and leave it different from the default. Below is the incomplete code I’m creating in windows 7: (everything inside the def function has to have identation) Obs: the program starts in from Tkinter import * and ends in the window.mainloop().

from tkinter import *

janela = Tk()

janela.title(" >>> Como criar os botão Maximizar? <<< ")

janela['bg'] = 'gray'

janela.wm_attributes('-fullscreen','true')

janela.geometry('340x400+500+200')

m = 0

def minimizar():

    janela.overrideredirect(False)

    janela.iconify()

    janela.wm_attributes('-fullscreen', 'True')

def fechar():

    janela.destroy()

def maximizar1():

    global m

    m = 0

    janela.overrideredirect(True)

    janela.geometry('1360x800+0+0')

def maximizar():

    global m

    m = 1

    janela.wm_attributes('-fullscreen', 'False')

    janela.overrideredirect(True)

    janela.geometry('340x300+500+200')

def maxi():

    print('1m=> ', m)

    if m == 0:

        maximizar()

    elif m == 1:

        maximizar1()

def move():

    pass


bt1 = Button(janela, text='Minimizar', font=("Helvetica", 14), bg='grey', command=minimizar)

bt1.grid(row=0, column=1)

bt2 = Button(janela, text='Maximizar', font=("Helvetica", 14), bg='grey', command=maxi)

bt2.grid(row=0, column=2)

bt3 = Button(janela, text='Sair', font=("Helvetica", 14), bg='grey', command=fechar)

bt3.grid(row=0, column=3)

janela.mainloop()

1 answer

0

Using only the tkinter I don’t know, but you can create buttons to manipulate windows using the library pygetwindow. See this example below:

from tkinter import Tk,Button
import pygetwindow

title = "Exemplo"

root = Tk()
root.title(title)
root.update() 
window = pygetwindow.getWindowsWithTitle(title)[0] # Obtém a janela através do título dela.

button = Button(root,width=15,text="Minimizar",command=window.minimize)
button.pack()

To download this library type pip install pygetwindow. I hope I’ve helped you ;)

  • Vlw, I’ll take a look at the pygetwindow. Thank you.

  • Not at all kkk. I happened to discover pygetwindow a few hours before I gave you the answer while I was researching pyautogui xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.