如何在图片前显示标签文字?

时间:2019-08-12 12:39:14

标签: python tkinter label

代码在下面,我的主要目标是当我按下“学习”按钮时(如您在图像中看到的那样),在选择一种方法之后,显示有关所选方法的文本和图像。但是,当我从Logistic回归页面切换到多项式回归页面时,Logistic回归页面上的图像区域会隐藏其他页面的文本。我已经使用了labelimage1.place_forget(),但是没有用。切换页面时,我需要在图像前面显示文本。

图片:

LogisticRegressionPage

PolynomialRegressionPage

PolynomialPageAfterLogisticRegression

代码是:

def HelpLogisticRegression():

trainlabel.config(font=("Times New Roman",9),text=("""To deal with outliers, Logistic Regression uses Sigmoid function.An explanation of logistic regression can begin with an explanation of the standard logistic function. The logistic function is a Sigmoid function, which takes any real value between zero and one. It is defined as"""))
trainlabel.place(x=180,
               y=35)

path1 = "lr1.png"
image1 = ImageTk.PhotoImage(Image.open(path1))
imagelabel1 = Label(helpml4, image = image1)
imagelabel1.place(x=275,y=130)
imagelabel1.config(bg="white")

trainlabel2.config(font=("Times New Roman",9),text=("And if we plot it, the graph will be S curve,"))
trainlabel2.place(x=180,
                   y=180)
path2 = "lr2.png"
image2 = ImageTk.PhotoImage(Image.open(path2))
imagelabel2 = Label(helpml4, image = image2)
imagelabel2.place(x=200,y=220)
imagelabel2.config(bg="white")
#    
trainlabel3.config(font=("Times New Roman",9),text="Let’s consider t as linear function in a univariate regression model.")
trainlabel3.place(x=180,
                   y=500)

path3 = "lr3.png"
image3 = ImageTk.PhotoImage(Image.open(path3))
imagelabel3 = Label(helpml4, image = image3)
imagelabel3.place(x=275,y=530)
imagelabel3.config(bg="white")


trainlabel4.config(font=("Times New Roman",9),text="So the Logistic Equation will become")
trainlabel4.place(x=685,
                   y=35)

path4 = "lr4.png"
image4 = ImageTk.PhotoImage(Image.open(path4))
imagelabel4 = Label(helpml4, image = image4)
imagelabel4.place(x=785,y=55)
imagelabel4.config(bg="white")


trainlabel5.config(font=("Times New Roman",9),text="Now, when logistic regression model come across an outlier, it will take care of it.")
trainlabel5.place(x=685,
                   y=150)

path5 = "lr6.png"
image5 = ImageTk.PhotoImage(Image.open(path5))
imagelabel5 = Label(helpml4, image = image5)
imagelabel5.place(x=600,y=180)
imagelabel5.config(bg="white")

trainlabel6.config(font=("Times New Roman",9),text="But sometime it will shift its y axis to left or right depending on outliers positions.")
trainlabel6.place(x=685,y=400)
# To show images it is required.

imagelabel1.configure(image=imagelabel1)

#   
def HelpPolynomialLinearRegression():
trainlabel.config(font=("Times New Roman",9),text=("""In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable x and the dependent variable y is modelled as an nth degree polynomial in x. Polynomial regression fits a nonlinear relationship between the value of x and the corresponding conditional mean of y, denoted E(y |x), and has been used to describe nonlinear phenomena such as the growth rate of tissues, the distribution of carbon isotopes in lake sediments, and the progression of disease epidemics. Although polynomial regression fits a nonlinear model to the data, as a statistical estimation problem it is linear, in the sense that the regression function E(y | x) is linear in the unknown parameters that are estimated from the data. For this reason, polynomial regression is considered to be a special case of multiple linear regression.The explanatory (independent) variables resulting from the polynomial expansion of the “baseline” variables are known as higher-degree terms. Such variables are also used in classification settings.Notice that PR is very similar to MLR, but consider at the same time instead of the different variables the same one X1, but in different powers; so basically we are using 1 variable to different powers of the same original variable."""))
trainlabel.place(x=180,
               y=35)

trainlabel2.config(font=("Times New Roman",9),text=(""))
trainlabel2.place(x=180,
                   y=430)
trainlabel3.config(font=("Times New Roman",9),text="")
trainlabel3.place(x=685,
                   y=35)


trainlabel4.config(font=("Times New Roman",9),text="")
trainlabel4.place(x=685,
                   y=95)


trainlabel5.config(font=("Times New Roman",9),text="")
trainlabel5.place(x=685,
                   y=390)
trainlabel6.config(text="")
# To show images it is required.

1 个答案:

答案 0 :(得分:0)

我已经解决了这样的问题,我在全局的define部分中添加了顶部,因此我定义了所有imagelabels之后,我销毁了所有的imagelabels(如果已定义),因为我不知道页面上是否有图像,所以我毕竟已经添加了imagelabel1.configure(image = imagelabel1)

def HelpLogisticRegression():
global imagelabel1,imagelabel2,imagelabel3,imagelabel4,imagelabel5,imagelabel6
try: imagelabel1.destroy()
except:print("Can't destroy 1")
try: imagelabel2.destroy()
except:print("Can't destroy 2")
try: imagelabel3.destroy()
except:print("Can't destroy 3")
try: imagelabel4.destroy()
except:print("Can't destroy 4")
try: imagelabel5.destroy()
except:print("Can't destroy 5")
try: imagelabel6.destroy()
except:print("Can't destroy 6")

trainlabel.config(font=("Times New Roman",9),text=("""To deal with outliers, Logistic Regression uses Sigmoid function.An explanation of logistic regression can begin with an explanation of the standard logistic function. The logistic function is a Sigmoid function, which takes any real value between zero and one. It is defined as"""))
trainlabel.place(x=180,
               y=35)

path1 = "lr1.png"
image1 = ImageTk.PhotoImage(Image.open(path1))
imagelabel1 = Label(helpframe, image = image1)
imagelabel1.place(x=275,y=130)
imagelabel1.config(bg="white")

trainlabel2.config(font=("Times New Roman",9),text=("And if we plot it, the graph will be S curve,"))
trainlabel2.place(x=180,
                   y=180)
path2 = "lr2.png"
image2 = ImageTk.PhotoImage(Image.open(path2))
imagelabel2 = Label(helpframe, image = image2)
imagelabel2.place(x=200,y=220)
imagelabel2.config(bg="white")
#    
trainlabel3.config(font=("Times New Roman",9),text="Let’s consider t as linear function in a univariate regression model.")
trainlabel3.place(x=180,
                   y=500)

path3 = "lr3.png"
image3 = ImageTk.PhotoImage(Image.open(path3))
imagelabel3 = Label(helpframe, image = image3)
imagelabel3.place(x=275,y=530)
imagelabel3.config(bg="white")


trainlabel4.config(font=("Times New Roman",9),text="So the Logistic Equation will become")
trainlabel4.place(x=685,
                   y=35)

path4 = "lr4.png"
image4 = ImageTk.PhotoImage(Image.open(path4))
imagelabel4 = Label(helpframe, image = image4)
imagelabel4.place(x=785,y=55)
imagelabel4.config(bg="white")


trainlabel5.config(font=("Times New Roman",9),text="Now, when logistic regression model come across an outlier, it will take care of it.")
trainlabel5.place(x=685,
                   y=150)

path5 = "lr6.png"
image5 = ImageTk.PhotoImage(Image.open(path5))
imagelabel5 = Label(helpframe, image = image5)
imagelabel5.place(x=600,y=180)
imagelabel5.config(bg="white")

trainlabel6.config(font=("Times New Roman",9),text="But sometime it will shift its y axis to left or right depending on outliers positions.")
trainlabel6.place(x=685,y=400)
# To show images it is required.

imagelabel1.configure(image=imagelabel1)
imagelabel2.configure(image=imagelabel2)
imagelabel3.configure(image=imagelabel3)
imagelabel4.configure(image=imagelabel4)
imagelabel5.configure(image=imagelabel5)