Python无法输出数据
本帖最后由 yunlongzhuhuo 于 2022-2-25 11:46 编辑import os, sys
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
from tkinter.messagebox import *
from tkinter.filedialog import askdirectory
class Application_ui(Frame):
#这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('视频下载工具')
self.master.geometry('363x239+543+330')
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
self.Label1Var = StringVar(value='视频地址:')
self.style.configure('TLabel1.TLabel', anchor='center', font=('Microsoft YaHei UI',9))
self.Label1 = Label(self.top, text='视频地址:', textvariable=self.Label1Var, style='TLabel1.TLabel')
self.Label1.setText = lambda x: self.Label1Var.set(x)
self.Label1.text = lambda : self.Label1Var.get()
self.Label1.place(relx=0.11, rely=0.167, relwidth=0.179, relheight=0.105)
global url
self.Text1Var = StringVar()
self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('Microsoft YaHei UI',9))
self.Text1.setText = lambda x: self.Text1Var.set(x)
url = lambda : self.Text1Var.get()
self.Text1.place(relx=0.309, rely=0.167, relwidth=0.554, relheight=0.105)
self.Label2Var = StringVar(value='视频保存地址:')
self.style.configure('TLabel2.TLabel', anchor='w', font=('Microsoft YaHei UI',9))
self.Label2 = Label(self.top, text='视频保存地址:', textvariable=self.Label2Var, style='TLabel2.TLabel')
self.Label2.setText = lambda x: self.Label2Var.set(x)
self.Label2.text = lambda : self.Label2Var.get()
self.Label2.place(relx=0.066, rely=0.469, relwidth=0.245, relheight=0.105)
global location
self.Text2Var = StringVar()
self.Text2 = Entry(self.top, textvariable=path, font=('Microsoft YaHei UI',9))
self.Text2.setText = lambda x: self.Text2Var.set(x)
location = lambda : self.Text2Var.get()
self.Text2.place(relx=0.331, rely=0.469, relwidth=0.399, relheight=0.105)
self.Command1Var = StringVar(value='浏览')
self.style.configure('TCommand1.TButton', font=('Microsoft YaHei UI',9))
self.Command1 = Button(self.top, text='浏览', textvariable=self.Command1Var, command=self.Command1_Cmd, style='TCommand1.TButton')
self.Command1.setText = lambda x: self.Command1Var.set(x)
self.Command1.text = lambda : self.Command1Var.get()
self.Command1.place(relx=0.771, rely=0.469, relwidth=0.135, relheight=0.105)
self.Command2Var = StringVar(value='下载')
self.style.configure('TCommand2.TButton', font=('Microsoft YaHei UI',9))
self.Command2 = Button(self.top, text='下载', textvariable=self.Command2Var, command=self.Command2_Cmd, style='TCommand2.TButton')
self.Command2.setText = lambda x: self.Command2Var.set(x)
self.Command2.text = lambda : self.Command2Var.get()
self.Command2.place(relx=0.397, rely=0.736, relwidth=0.201, relheight=0.172)
class Application(Application_ui):
#这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
def __init__(self, master=None):
Application_ui.__init__(self, master)
def Command1_Cmd(self, event=None):
path_ = askdirectory()
path.set(path_)
def Command2_Cmd(self, event=None):
a = location()
print(a)
b = url()
print(b)
if __name__ == "__main__":
top = Tk()
path = StringVar()
Application(top).mainloop()
就是上面这段代码,第76行的print(a)无法输出数据,但下面的print(b)却能输出,为什么?
像如图所示的那样:
断点看看a有没有值。也许就是空值。 wkfy 发表于 2022-2-25 10:51
断点看看a有没有值。也许就是空值。
我试了,下断点停不下来。
我用type(a)试了一下,什么也没返回 第45行改成:
self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('Microsoft YaHei UI', 9))
需要绑定到Text2Var变量 根据你的思路,只需要把self.Text2Var变量成path即可,也就是
44行改为
# self.Text2Var = StringVar()
46行改为
self.Text2.setText = lambda x: path.set(x)
47行改为
location = lambda : path.get() zfb 发表于 2022-2-25 11:41
根据你的思路,只需要把self.Text2Var变量成path即可,也就是
44行改为
# sel ...
感谢感谢,问题解决了
页:
[1]