私はスクリプト用のGUIを作成しています。私はまず、空のGUI(以下に示すコード)を開発しました。画像は表示されませんでしたが、私はグーグルで、画像への参照がガベージコレクションされていることを認識し、このリンク(http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm)に従って固定しました。私はそのイメージをGUIスクリプト内のグローバル変数にしました。私のpython tkinter GUIが別のスクリプトにインポートされたときに画像を表示しないのはなぜですか?
このファイルはGUI.pyと呼ばれている:私は私の実際のテスト・スクリプトにこのGUI.pyをインポートするときに
import Tkinter as tk
import ttk
import tkMessageBox
import time
from PIL import ImageTk, Image
class StartPage(tk.Frame):
def __init__(self, master, text, height, width, *args, **kwargs):
global logo
tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
self.height = height
self.width = width
# path = "test.jpg"
# self.img = ImageTk.PhotoImage(Image.open(path))
logo = ImageTk.PhotoImage(Image.open('test.jpg'))
self.picture = tk.Label(self, image=logo)
#self.picture.image = img
self.picture.pack(side = "bottom", fill = "both", expand = "yes")
label = tk.Label(self, text='Waiting', font=("Helvetica bold", 24)).pack()
label = tk.Label(self, text='Click START Button', font=("Helvetica", 16)).pack(expand=True)
button = tk.Button(self, text=text, font=('Helvetica', 20),
command=lambda: self.callback())
button.pack(side="top", expand=True)
root.update()
def onlift(self):
root.geometry('{}x{}'.format(self.width, self.height))
self.lift()
class TestPage(tk.Frame):
def __init__(self, master, text, height, width, *args, **kwargs):
tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
self.height = height
self.width = width
self.state = tk.StringVar()
self.label = tk.Label(self, textvariable=self.state, font=("Helvetica", 16)).pack()
self.progress = ttk.Progressbar(self, orient='horizontal', length=1000, mode='determinate')
self.progress.pack()
path = 'connect.jpg'
img = ImageTk.PhotoImage(Image.open(path))
self.picture = tk.Label(self, text='test image', image=img)
self.picture.image = img
self.picture.pack()
root.update()
def onlift(self):
global p1
root.geometry('{}x{}'.format(self.width, self.height))
self.lift()
self.progress["value"] = 0
self.state.set('Running...')
root.update()
time.sleep(1)
self.progress["value"] = 50
root.update()
confirm = tkMessageBox.askyesno(message='ON?', icon='question', title='Confirmation')
#print confirm
if confirm:
print 'Confirmed'
self.progress["value"] = 100
self.state.set('PASSED!')
root.update()
tkMessageBox.showinfo(title='Test Passed',message='PASS')
self.label
else:
self.state.set('Test FAILED!')
root.update()
tkMessageBox.showinfo(title='Test Failed',message='FAIL',icon='warning')
p1.onlift()
class App(tk.Frame):
def __init__(self, *args, **kwargs):
global p1
tk.Frame.__init__(self, *args, **kwargs)
p1 = StartPage(self, 'START', height=root.winfo_screenheight(), width=root.winfo_screenwidth())
p2 = TestPage(self, 'blank', height=root.winfo_screenheight(), width=root.winfo_screenwidth())
p1.callback = p2.onlift
p2.callback = p1.onlift
p1.place(x=0, y=0, relwidth=1, relheight=1)
p2.place(x=0, y=0, relwidth=1, relheight=1)
p1.onlift()
global p1
global p2
global logo
global connectimg
root = tk.Tk()
root.title('GUI')
app = App(root)
root.update()
私の問題は今、私は再び画像を失います。私はそのスクリプトでイメージを読み込み、GUIへの参照を渡そうとしましたが、それはうまくいきませんでした。私は自分自身で)メインループ(とGUIを実行する画像が罰金を表示した場合
GUI.p2.state.set('Running')
GUI.p2.progress["value"] = 50
GUI.root.update()
:
import GUI
は、これは私が私のスクリプトからラベルテキストと、プログレスバーの値を変更する方法です。 2番目のスクリプトからインポートしたモジュールとして実行すると、イメージは表示されません。私は間違って何をしていますか?
すべてのコードを実行できるようにテストスクリプトを表示します。 – furas
実際のテストスクリプトもウィジェットを作成しますか?一度に1つの 'Tk'インスタンスしか実行できません。申し訳ありませんが –
@furasは、テスト・スクリプトはのみのダイアログ – aroushan