[英]Program is not printing textvariable in tkinter label when importing it as a module in another file
我有一個文件 example.py,我想將它導入另一個名為 gsm_call.py 的文件中。 當我單獨執行 example.py 時,它在標簽中打印“abcd”,但是當我將它作為模塊導入時,tkinter 窗口打開但不打印“abcd”。 為什么這是文件作為主文件和作為模塊的不同行為。 什么是解決方案。
例子.py
`from Tkinter import *
pk=Tk()
var=StringVar()
var.set('abcdd')
label = Label(pk, textvariable=var,font=(25),bg='Orange')
label.place(height=600,width=1370,x=0,y=0)
pk.mainloop()`
gsm_call.py
`# -*- coding: utf-8 -*-
from Tkinter import *
from PIL import Image, ImageTk
class pk():
s = ' '
def printt(self):
import example
def __init__(self,master):
self.new_num = True
self.s=''
self.text_box=Entry(master,justify=RIGHT)
self.text_box.place(height=100,width=1350,x=8,y=0)
def num_press(self, num):
global s
temp = self.text_box.get()
temp2 = str(num)
if self.new_num:
self.current = temp2
self.current = temp + temp2
self.display(self.current)
self.s += str(num)
self.b=num
def printlist(self):
print self.b
def display(self, value):
self.text_box.delete(0, END)
self.text_box.insert(0, value)
class GSM_call(Frame):
def __init__(self, parent,controller,GSM):
Frame.__init__(self, parent)
c =Canvas(self, bg='White', height=700, width=1400,bd=0)
c.pack()
global p1
p1=pk(self)
button1 = Button(self, bd=20,text="1",fg='White',bg="Black",command=lambda : p1.num_press(1))
button1.place(height=150,width=450,x=10,y=100)
button2 = Button(self, text="2",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(2))
button2.place(height=150,width=450,x=460,y=100)
button3 = Button(self, text="3",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(3))
button3.place(height=150,width=450,x=910,y=100)
button4 = Button(self, text="4",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(4))
button4.place(height=150,width=450,x=10,y=250)
button5 = Button(self, text="5",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(5))
button5.place(height=150,width=450,x=460,y=250)
button6 = Button(self, text="6",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(6))
button6.place(height=150,width=450,x=910,y=250)
button7 = Button(self, text="7",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(7))
button7.place(height=150,width=450,x=10,y=400)
button8 = Button(self, text="8",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(8))
button8.place(height=150,width=450,x=460,y=400)
button9 = Button(self, text="9",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(9))
button9.place(height=150,width=450,x=910,y=400)
button0 = Button(self, text="0",bd=20,fg='White',bg="Black",command=lambda: p1.num_press(7))
button0.place(height=150,width=450,x=460,y=550)
button_call = Button(self, text="call",bd=20,fg='White',bg="Black",command=lambda: p1.printt())
button_call.place(height=150,width=450,x=10,y=550)
button1st = Button(self, text="Back",bd=20,fg='White',bg="Black",
command=self.Abc())
button1st.place(height=150,width=450,x=910,y=550)
def Abc(self):
#a=lambda: p1.printt()
print 'a'
if __name__=='__main__':
ck.mainloop()`
example.py 使用類
`from Tkinter import *
pk=Tk()
class Exp():
def __init__(self,master):
self.var=StringVar()
self.var.set('abcdd')
label = Label(master, textvariable=self.var,font=(25),bg='Orange')
label.place(height=600,width=1370,x=0,y=0)
par=Exp(pk)
pk.mainloop()`
問題是你有兩個根窗口。 一個 tkinter 程序必須正好有一個根窗口,並且只調用一次mainloop
1 。 您需要修改 example.py 以創建Toplevel
的實例而不是Tk
的實例,並從該文件中刪除對mainloop
的調用。
1這不是字面上的真實,但擁有多個很少是一個好主意,因為它會產生諸如您正在經歷的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.