【问题标题】:tkinter python gui background not showing [duplicate]tkinter python gui背景不显示[重复]
【发布时间】:2017-08-20 13:20:23
【问题描述】:

我的基本问题是试图展示我的背景。

它会在不包含额外代码时加载并显示,但一旦我尝试在循环中运行一些其他进程,后台就会停止加载/显示

更进一步的背景是,我已经为我的学位项目生成了运行批处理的代码。当现在尝试将 GUI 合并到这个我正在尝试 Tkinter 时,请记住这对我来说是全新的,所以在搜索类似问题/纠正时我可能错过了几点。

当我尝试构建 GUI 学习时,我尝试了一个非常基本的东西来构建它,如下所示:

import time
from tkinter import *
import tkinter as Tk   ## notice lowercase 't' in tkinter here

root = Tk.Tk()
root.wm_geometry("1024x768")
root.title('Rpi Automated MicroBrewery')

a = StringVar()
a.set("1234")

text1 = StringVar()
text1.set("Header Full")

def startBrew():
    print('brew has started')

def a():
    print('a')
def b():
    print('b')
def c():
    print('c')

def main():
    a()
    b()
    c()

class Application(Tk.Frame):
    def __init__(self, master=None):
        Tk.Frame.__init__(self, master)
        self.createWidgets()

    def createWidgets(self):

        background_image = Tk.PhotoImage(file="C:\\Users\\chris\\Documents\\Uni\\Year Four\\Final Year Project\\bg.png")
        self.background_label = Tk.Label(root, image=background_image).place(x=0, y=0, relwidth=1, relheight=1)

        self.startButton = Tk.Button(root, text="Start Brewing!", command=startBrew).place(x=200,y=200)
        self.parametersButton = Tk.Button(root, text="Brewing Parameters", command=Application.parametersWindow).place(x=300,y=300)

        self.headerTemp = Tk.Label(root, textvariable=a).place(x=500,y=500)
        self.headerLev = Tk.Label(root, textvariable=text1).place(x=500,y=600)

        self.onUpdate()

    def parametersWindow():
        top = Toplevel()
        top.maxsize(400,400)
        top.title("Adjust Brew Parameters")
        about_message = "put some parameters in here"

        msg = Message(top, text=about_message).place(x=0,y=0)

        button = Button(top, text="Dismiss", command=top.destroy)

    def onUpdate(self):
        main()
        self.after(1000, self.onUpdate)

app = Application(master=root)
root.mainloop()

请仔细检查并在必要时更正。但请记住,这对我来说是全新的,如果答案太复杂,可能会超出我的想象。也知道为什么 text1 string var 会显示,但我根本无法显示 'a'。

我已经尝试过这个,但它运行 main() 直到被取消,然后允许显示背景和 GUI。请帮助我确定这是我不遵循的简单规则:

    import time
from tkinter import *
import tkinter as tk   ## notice lowercase 't' in tkinter here

root = tk.Tk()
root.wm_geometry("1024x768")
root.title('Rpi Automated MicroBrewery')

a = StringVar()
a.set("1234")

text1 = StringVar()
text1.set("Header Full")

def startBrew():
    print('brew has started')

def a():
    print('a')
def b():
    print('b')
def c():
    print('c')

def main():
    a()
    b()
    c()
    root.after(5000, main())

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.createWidgets()

    def createWidgets(self):

        self.startButton = tk.Button(root, text="Start Brewing!", command=startBrew).place(x=200,y=200)
        self.parametersButton = tk.Button(root, text="Brewing Parameters", command=Application.parametersWindow).place(x=300,y=300)

        self.headerTemp = tk.Label(root, textvariable=a).place(x=500,y=500)
        self.headerLev = tk.Label(root, textvariable=text1).place(x=500,y=600)

        self.onUpdate()

    def parametersWindow():
        top = Toplevel()
        top.maxsize(400,400)
        top.title("Adjust Brew Parameters")
        about_message = "put some parameters in here"

        msg = Message(top, text=about_message).place(x=0,y=0)

        button = Button(top, text="Dismiss", command=top.destroy)

    def onUpdate(self):
        main()
        self.after(5000,onUpdate)

background_image = tk.PhotoImage(file="C:\\Users\\chris\\Documents\\Uni\\Year Four\\Final Year Project\\bg.png")
background_label = tk.Label(root, image=background_image).place(x=0, y=0, relwidth=1, relheight=1)
app = Application(master=root)
root.mainloop()

谢谢

【问题讨论】:

    标签: python user-interface tkinter background


    【解决方案1】:

    这是一个常见的问题。您需要保留图像参考,否则它会被垃圾收集。最简单的方法是通过添加“self”使其成为实例变量。到变量名:

    self.background_image = Tk.PhotoImage(file="C:\\Users\\chris\\Documents\\Uni\\Year Four\\Final Year Project\\bg.png")
    self.background_label = Tk.Label(root, image=self.background_image)
    self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
    

    另外,如果您想避免以后出现错误,请不要将布局与初始化放在同一行。像我上面那样把它分成两行。

    要回答您的其他问题,“a”变量未显示,因为您稍后在代码中定义了一个函数“a”,它覆盖了 StringVar“a”。让这成为你不要使用单字母变量名的教训。始终使用描述性名称。

    【讨论】:

    • 谢谢!我想你不想帮我做剩下的事?? :')
    • 你看到我的编辑了吗?还有别的吗?如果您有更多问题,您可能想尝试 reddit.com/r/learnpython,因为它更面向初学者。
    • 谢谢。我现在看看。有时很难将这些示例与我想要实现的目标联系起来。由于我正在努力的最后期限,我现在几乎摸不着头脑。再次感谢您的帮助。
    猜你喜欢
    • 2012-07-31
    • 2017-04-27
    • 1970-01-01
    • 2017-10-07
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多