我一直在到處尋找這種簡單的解決方案,但我仍然缺乏。
我要做的只是將一些文本僅在螢屏上顯示,沒有背景或使用帶有選單欄/按鈕的“訊息框”。只是文字,僅此而已。也許有一種方法可以為字體設定顏色,并且我希望文本為淺灰色。使用 Windows 10 和 Python 3.8 。
我看過easygui(訊息框,使用按鈕)、PySimpleGUI(目前最好的選擇,如下所示,但默認為白色背景)、Tkinter 和 pygame(但我不知道如何設定我正在尋找的內容對于那些包。
這就是我使用 PySimpleGui 所擁有的,如果可能的話,我希望這是一個透明的背景!
def popup_message(message):
sg.popup(str(message),
title = None,
button_color = 'white',
background_color = None,
text_color = 'grey85',#'light gray',
button_type = 5,
auto_close = True,
auto_close_duration = 20,
custom_text = (None, None),
non_blocking = False,
icon = None,
line_width = None,
font = None,
no_titlebar = True,
grab_anywhere = True,
keep_on_top = True,
location = (None, None),
relative_location = (250, 250),
any_key_closes = True,
image = None,
modal = True)
更新
This is the closest thing I've got with using Tkinter. Problem now is the menu bar is still visible, and using the Label class results in a solid background. HOW CAN I ONLY SHOW TEXT ON SCREEN IN PYTHON!? I'm not even sure I'm going about this the right way, but seems like a pretty basic idea?
# Import the Tkinter Library
from tkinter import *
# Create an instance of Tkinter Frame
root = Tk()
# Create a Label to print some text
label = Label(root, text="This is a New Line Text", font= ('Helvetica 14 bold'), foreground= "red3")
label.pack()
# Create a transparent window
root.wm_attributes('-transparentcolor','#add123')
# Set the geometry of window
root.geometry("700x350")
# Add a background color to the Main Window
root.config(bg = '#add123')
root.mainloop()

uj5u.com熱心網友回復:
不支持Textin的透明背景。popup
在 PySimpleGUI 中需要和自己定義的新彈出功能。
background_color將元素的選項設定Text為一種指定的透明度顏色,例如“#add123”。- 將選項設定為透明度的指定顏色
transparent_color。Window - 設定選項
no_titlebar以Window隱藏標題欄。
以下代碼顯示了方式
import PySimpleGUI as sg
def popup(message):
global win
if win:
win.close()
layout = [[sg.Text(message, background_color=bg, pad=(0, 0))]]
win = sg.Window('title', layout, no_titlebar=True, keep_on_top=True,
location=(1000, 200), auto_close=True, auto_close_duration=3,
transparent_color=bg, margins=(0, 0))
event, values = win.read(timeout=0)
return win
bg = '#add123'
sg.set_options(font=("Courier New", 24))
layout = [[sg.Button('POPUP')]]
window = sg.Window('title', layout)
win = None
while True:
event, value = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'POPUP':
win = popup('Here is the message.')
window.force_focus()
if win:
win.close()
window.close()
uj5u.com熱心網友回復:
您正在嘗試制作一種沒有title-menu.In 的螢屏,在這種情況下,我可以通過告訴您使用root.overrideredirect(True)哪個是代碼的關鍵部分來幫助您。
uj5u.com熱心網友回復:
您有兩個選項來繪制沒有背景的文本。第一種是使用畫布繪制文本,如下所示。使用這種方法,如果您關心可調整性,則每次調整視窗大小時都必須自己將文本居中。
canvas.create_text(x, y, "Text with no bg!")
第二種方法是使用一個禁用的按鈕,里面有一個空的 img。
img = PhotoImage(file="file.png")
b = tk.Button(root, text="My Text", image=img, relief="FLAT", state="DISABLED")
b.image = img
b.pack()
有關更多資訊,請參閱此答案。如果您還有其他問題,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442984.html
