【问题标题】:The button text and color does not appear on Tkinter program按钮文本和颜色不会出现在 Tkinter 程序上
【发布时间】:2019-05-08 21:21:05
【问题描述】:

该程序在运行 windows 或 ubuntu 的其他计算机上运行良好,但文本不会显示在按钮上。这是一段较长代码的 sn-p:

from Tkinter import *

from math import atan
from math import log
import math
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt



# --- function ---


def create_first_frame():
    global root
    global frame

    # frame.destroy()

    frame = Frame(bg="blue")
    frame.pack()

    label1 = Label(frame, text="hello!", fg="white", bg="blue", font="normal 30")
    label1.pack()

    button1 = Button(frame, text="Enter", fg="white", bg="blue", font="normal 20")
    button1.pack()

    button2 = Button(frame, text="Exit", font="normal", fg="white", bg="red", command=root.destroy)

    button2.pack(side=LEFT)




root = Tk()
create_first_frame()
root.mainloop()

我们期待显示“开始”和“退出”等词。

我们只希望按钮的颜色与文本一起显示

【问题讨论】:

  • 遗憾的是,Tkinter Buttons 在 macOS 上没有太大的灵活性,您可以更改 fg 但无法更改 bg。这是因为 macOS ui 更改的限制和访问受限。看到这个post

标签: python macos tkinter


【解决方案1】:

我不确定问题是什么。我取出了不必要的导入,然后跑了

from tkinter import *

def create_first_frame():
    global root
    global frame

    # frame.destroy()

    frame = Frame(bg="blue")
    frame.pack()

    label1 = Label(frame, text="hello!", fg="white", bg="blue", font="normal 30")
    label1.pack()

    button1 = Button(frame, text="Enter", fg="white", bg="blue", font="normal 20")
    button1.pack()

    button2 = Button(frame, text="Exit", font="normal", fg="white", bg="red", command=root.destroy)

    button2.pack(side=LEFT)

root = Tk()
create_first_frame()
root.mainloop()

为我创造:

这是您指定的颜色...

【讨论】:

  • 我同意这个问题并不清楚,但我认为 OP 想知道为什么它不能在运行 macos 的系统上工作(但在 windows 或 ubuntu 上)。
猜你喜欢
  • 2020-04-21
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 2017-12-19
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
相关资源
最近更新 更多