简体   繁体   English

为什么我的标题栏没有出现在我的 Tkinter 消息框中?

[英]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.我在装有 Big Sur 的 Mac 上使用 Python 3 和 Tkinter 使用 VS Code。 The title bar shows up on:标题栏出现在:

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

But, for some reason, they're not showing up on my messageboxes :但是,出于某种原因,它们没有出现在我的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.这是一个基本程序,我不确定它是代码、VS 代码、Python 还是我无意中更改的某些设置。 Any help would be great!!任何帮助都会很棒!

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

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.此选项在 Mac OS X 上被忽略,平台指南禁止在此类对话框中使用标题。

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看起来这就是 tkinter 在 Mac OS X 中的实现方式。试试这段代码,它在我的 OpenBSD 机器上显示了一个标题栏

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:我在我的 Mac 上测试过,这是我得到的 output:

在此处输入图像描述

  • Darwin Kernel Version 17.7达尔文 Kernel 版本 17.7
  • Python 3.8.3 Python 3.8.3
  • Tk 8.6 Tk 8.6

I tried your program on my Mac, the title bar shows in the message box.我在我的 Mac 上试过你的程序,标题栏显示在消息框中。 It's most likely a setting or version problem很有可能是设置或版本问题

图片

Try to manually set the args title like:尝试手动设置 args title ,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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