简体   繁体   中英

Why is my title bar not showing up in my Tkinter messagebox?

I'm working in VS Code with Python 3 and Tkinter on a Mac with Big Sur. The title bar shows up on:

root = Tk()
root.title("Password Manager")

But, for some reason, they're not showing up on my messageboxes :

messagebox.showwarning("Oops!", "Please don't leave any boxes empty.")

It's a basic program and I'm not sure if it's the code, VS Code, Python or some setting that I've inadvertently changed along the way. Any help would be great!!

Window & message box screenshot: 窗口和消息框截图

Here's my full code, for info:

from tkinter import *
from tkinter import messagebox
from random import choice, randint, shuffle
import pyperclip

LABEL_FONT = "Adobe Gothic Std"

# ---------------------------- PASSWORD GENERATOR ------------------------------- #


def generate_password():
    letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

    password_list = [choice(letters) for char in range(randint(8, 10))]
    password_list += [choice(symbols) for char in range(randint(2, 4))]
    password_list += [choice(numbers) for char in range(randint(2, 4))]
    shuffle(password_list)

    password = "".join(password_list)
    password_entry.insert(0, password)
    pyperclip.copy(password)

# ---------------------------- SAVE PASSWORD ------------------------------- #


def save():
    website_name = website_entry.get()
    email_address = email_entry.get()
    new_password = password_entry.get()

    if len(website_name) == 0 or len(new_password) == 0:
        messagebox.showwarning("Oops!", "Please don't leave any boxes empty.")
    else:
        is_ok = messagebox.askokcancel(title=website_name, message=f"These are the details entered: \nEmail: {email_address} "
                                       f"\nPassword: {new_password} \nIs it ok to save?")
        if is_ok:
            with open("my_passwords.txt", "a") as data_file:
                data_file.write(f"{website_name} *|* {email_address} *|* {new_password}\n")
                website_entry.delete(0, END)
                password_entry.delete(0, END)


# ---------------------------- UI SETUP ------------------------------- #
root = Tk()
root.title("Password Manager")
root.config(padx=50, pady=50)

canvas = Canvas(height=200, width=200)
logo_img = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=logo_img)
canvas.grid(column=1, row=0)

# Labels
website_label = Label(text="Website:", font=(LABEL_FONT, 15))
website_label.grid(column=0, row=1, sticky=E)

email_label = Label(text="Email/Username:", font=(LABEL_FONT, 15))
email_label.grid(column=0, row=2, sticky=E)

password_label = Label(text="Password:", font=(LABEL_FONT, 15))
password_label.grid(column=0, row=3, sticky=E)

# Entry Boxes
website_entry = Entry(width=37)
website_entry.grid(column=1, row=1, columnspan=2, pady=5)
website_entry.focus()

email_entry = Entry(width=37)
email_entry.grid(column=1, row=2, columnspan=2, pady=5)
email_entry.insert(0, "designguru14-shoppie@yahoo.com")

password_entry = Entry(width=21)
password_entry.grid(column=1, row=3, sticky=W, pady=5)

# Buttons
password_button = Button(text="Generate Password", padx=5, pady=5, command=generate_password)
password_button.grid(column=2, row=3)

add_info_button = Button(text="Add", width=38, pady=5, command=save)
add_info_button.grid(column=1, row=4, columnspan=2, sticky=W)

root.mainloop()

The documentations states that

This option is ignored on Mac OS X, where platform guidelines forbid the use of a title on this kind of dialog.

Looks like this is how tkinter is implemented in Mac OS X. Try this code, which shows a title bar on my on my OpenBSD machine

import sys
import os
from tkinter import messagebox

if sys.version_info[0] < 3:
    import Tkinter as tkinter
else:
    import tkinter
 
window = tkinter.Tk()
window.geometry('640x380')

messagebox.showwarning("Oops!", "I did it again")
window.mainloop()

I tested it on my Mac, this is the output I get:

在此处输入图像描述

  • Darwin Kernel Version 17.7
  • Python 3.8.3
  • Tk 8.6

I tried your program on my Mac, the title bar shows in the message box. It's most likely a setting or version problem

图片

Try to manually set the args title like:

messagebox.showwarning(title='Oops!', message="Please don't leave any boxes empty.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM