2017-03-27 1 views
0

내 기본 문제는 내 배경을 표시하려고하는 것입니다.tkinter python gui 배경이 보이지 않음

IT 부하 및 추가 코드 있지만 최대한 빨리

을 보여주는/시도하고 배경로드 중지 루프에서 다른 프로세스를 실행으로 더 배경의 비트는 내가 코드를 생산 한 것입니다을 포함하지 않을 때 표시 내 학위 프로젝트에 배치 프로세스를 실행합니다. Tkinter를 시도하는이 GUI에 GUI를 통합하려고 할 때이 점을 염두에두면 새로운 것이므로 비슷한 문제/정류장을 검색 할 때 몇 가지 사항을 놓친 것일 수 있습니다.

내가 그 다음과 같습니다에 구축 할 아주 기본적인 일을 시도했습니다 가서 내가 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() 

가 면밀히 검토하시기 바랍니다 및 올바른 곳이 켜지지. 그러나 내게 모두 새로운 것이라는 점을 명심하십시오. 대답이 너무 복잡하면 제 머리 위로 갈 수 있습니다. 또한 왜 텍스트 1 문자열 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() 

감사

답변

0

이 공통 잡았다입니다 : 그 간단한 규칙 내가 다음과 같은 아니에요 메신저 있는지 도와주세요. 이미지 참조를 유지해야하거나 가비지 수집됩니다. 가장 쉬운 방법은 '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) 

, 초기화와 같은 줄에 레이아웃을 넣지 마십시오. 위에서 한 것처럼 두 줄로 나눕니다.

다른 질문에 대답하기 위해 StringVar "a"를 덮어 쓰는 코드에서 나중에 "a"함수를 정의했기 때문에 "a"변수가 표시되지 않습니다. 이것이 단일 문자 변수 이름을 사용하지 않는 교훈이되게하십시오. 항상 구체적인 이름을 사용하십시오.

+0

감사! 나는 당신이 나머지와 함께 나를 도와주는 것을 공상적으로 생각하지 않는다? : ') – chrisn89

+0

내 편집을 보았습니까? 다른 뭔가 있었나요? 초보자 용이므로 reddit.com/r/learnpython을 사용해보십시오. – Novel

+0

나는 고마워했다. 나는 지금 볼 것이다. 때로는 내가 성취하려고하는 것에 그 예를 관련시키기가 어렵습니다. 나는 지금 일하고있는 마감 기한 때문에 그 순간에 거의 멍청하다. 도움에 다시 한번 감사드립니다. – chrisn89