gpt4 book ai didi

Python:打开文件对话框没有出现,因为无法访问文本变量

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:49 24 4
gpt4 key购买 nike

我已经为文件对话框创建了一个按钮、一个条目和一个函数。在我按下“浏览”按钮后,文件对话框没有出现,所以我如何确保我的 custName.set(filename) 可以在我的 TracingMethod () 函数中使用。

创建文件对话框的步骤:

第 1 步:从 tkinter 导入文件对话框

from tkinter import filedialog

第二步:创建按钮和条目

self.browseButton = Button(self.radioframe, text="Browse",command = self.open)
self.browseButton.grid(row =3, column =3, sticky = W)

#Define the entry
custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = custName)
location_ent.grid(row =2, column = 2, sticky = W,columnspan=1)
location_ent.update()
location_ent.focus_set()

第三步:创建open()函数【IDE在这里显示错误】

def open():
filename = filedialog.askopenfilename(parent=root,title='Choose a file')
custName.set(filename)

我在想的是我是在独家新闻之外定义函数吗?所以我提供了完整的代码以供引用。

from tkinter import *
from tkinter import filedialog

class TracingInterface(Frame):
def __init__(self, master):
super().__init__()
master.minsize(width=700, height=520)
master.maxsize(width=700, height=520)
Grid.config(self)
self.TracingMethod()
self.logDetails()
self.otherFunctionInterface()


def TracingMethod(self):

self.traceMethodSelect = StringVar()
self.traceMethodSelect.set("LT")

self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =300)
self.radioframe.grid(row= 0, column=0)
self.radioframe.grid_propagate(0)

self.radioframe.LT= Radiobutton(
self.radioframe, text="Live Tracing",
variable=self.traceMethodSelect, value="LT",
anchor=W).grid(row=1, column = 0, sticky = W,columnspan=2)
self.radioframe.SL= Radiobutton(
self.radioframe, text="Specific Location",
variable=self.traceMethodSelect, value="SL",
anchor=W).grid(row=2, column = 0, sticky = W,columnspan=2)

self.traceButton = Button(self.radioframe, text="Trace")
self.traceButton.grid(row =3, column =0, sticky = W)

self.cancelButton = Button(self.radioframe, text="Cancel")
self.cancelButton.grid(row =3, column =1, sticky = W)

self.configUAButton = Button(self.radioframe, text="Configuration",command=printMessage)
self.configUAButton.grid(row =3, column =2, sticky = W)
self.configUAButton.config(width=15)

self.browseButton = Button(self.radioframe, text="Browse",command = self.open)
self.browseButton.grid(row =3, column =3, sticky = W)

custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = custName)
location_ent.grid(row =2, column = 2, sticky = W,columnspan=1)
location_ent.update()
location_ent.focus_set()

def logDetails(self):
self.logframe = LabelFrame(self,text="Log Details",height= 520,width =390,padx=15)
self.logframe.grid_propagate(0)

self.logframe.grid_rowconfigure(0,weight =1)
self.logframe.grid_columnconfigure(0,weight=1)

xscrollbar = Scrollbar(self.logframe,orient = HORIZONTAL)
xscrollbar.grid(row=1, column=1, sticky=E+W,columnspan=2)

yscrollbar = Scrollbar(self.logframe,orient= VERTICAL)
yscrollbar.grid(row=0, column=3, sticky=N+S)

text = Text(self.logframe,width=50,height=50, wrap=NONE, xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
text.grid(row=0, column=1, columnspan=2)
# attach listbox to scrollbar
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)

button_1 = Button(self.logframe, text="View", command=printMessage,width =10)
button_1.grid(row=2,column= 1)

button_2 = Button(self.logframe, text="Export", command=printMessage,width =10)
button_2.grid(row=2,column= 2)

self.logframe.grid(row=0,column =1,rowspan=5)

def otherFunctionInterface(self):
self.otherFrame = LabelFrame(self,text="Other Function",height= 400,width =300)
self.otherFrame.grid(row=4, column=0)
self.otherFrame.grid_propagate(0)

OpenPreviousCaseFile = Button(self.otherFrame, text="Open previous case file", command=printMessage,height = 4,
width =21)
OpenPreviousCaseFile.grid(row=5,column= 0,pady=5,padx=5)

OpenPreviousTracingResult = Button(self.otherFrame, text="Open last tracing result ", command=printMessage,
height = 4, width =21)
OpenPreviousTracingResult.grid(row=6,column= 0,pady=5,padx=5)

OpenMenualbtn = Button(self.otherFrame, text="User manual", command=printMessage,height =4, width =21)
OpenMenualbtn.grid(row=7,column= 0,pady=5,padx=5)

AboutBtn = Button(self.otherFrame, text="About", command=printMessage,height = 4, width =21)
AboutBtn.grid(row=8,column= 0,pady=5,padx=5)

locateCaseFile = Entry(self.otherFrame)
locateCaseFile.grid(row=5,column = 1)

locateTraceResult = Entry(self.otherFrame)
locateTraceResult.grid(row=6,column = 1)
def open():
filename = filedialog.askopenfilename(parent=root,title='Choose a file')
custName.set(filename)


def printMessage():
print("Wow this actually worked!")

if __name__=='__main__':
root=Tk()
root.title("Windows User Activity History Tracing and Analysing System")
tif= TracingInterface(root)
root.mainloop()

最佳答案

self.browseButton 小部件没有出现,因为您将 location_entcolumnspan 选项设置为 1。您必须将其修改为 2,因为您有 自己。 configUAButton 按钮也在那里。

这意味着您需要更改:

location_ent.grid(row =2, column = 2, sticky = W,columnspan=1)

收件人:

location_ent.grid(row =2, column = 2, sticky = W,columnspan=2)

但是您现在几乎不能self.browseButt,因为您为self.radioframe 设置的宽度不够。所以改变这一行:

self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =300)

到:

self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =355)

当然,您需要相应地修改master 窗口的width 选项。为此,更改:

master.minsize(width=700, height=520)
master.maxsize(width=700, height=520)

最后,您无法访问 custName,因为您需要将其设为 instance variable反而。这意味着您需要更改:

self.custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = self.custName)

收件人:

self.custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = self.custName)

收件人:

master.minsize(width=755, height=520)
master.maxsize(width=755, height=520)

并且(因此)在 open() 方法中:

custName.set(filename)

收件人:

self.custName.set(filename)

完整程序

让我们把前面提到的所有内容放在一起:

from tkinter import *
from tkinter import filedialog

class TracingInterface(Frame):
def __init__(self, master):
super().__init__()
master.minsize(width=755, height=520)
master.maxsize(width=755, height=520)
Grid.config(self)
self.TracingMethod()
self.logDetails()
self.otherFunctionInterface()


def TracingMethod(self):

self.traceMethodSelect = StringVar()
self.traceMethodSelect.set("LT")
# Bill
self.radioframe = LabelFrame(self,text="Tracing Method",height= 120,width =355)
self.radioframe.grid(row= 0, column=0)
self.radioframe.grid_propagate(0)

self.radioframe.LT= Radiobutton(
self.radioframe, text="Live Tracing",
variable=self.traceMethodSelect, value="LT",
anchor=W).grid(row=1, column = 0, sticky = W,columnspan=2)
self.radioframe.SL= Radiobutton(
self.radioframe, text="Specific Location",
variable=self.traceMethodSelect, value="SL",
anchor=W).grid(row=2, column = 0, sticky = W,columnspan=2)

self.traceButton = Button(self.radioframe, text="Trace")
self.traceButton.grid(row =3, column =0, sticky = W)

self.cancelButton = Button(self.radioframe, text="Cancel")
self.cancelButton.grid(row =3, column =1, sticky = W)

self.configUAButton = Button(self.radioframe, text="Configuration",command=printMessage)
self.configUAButton.grid(row =3, column =2, sticky = W)
self.configUAButton.config(width=15)

self.browseButton = Button(self.radioframe, text="Browse",command = self.open)
self.browseButton.grid(row =3, column =3, sticky = W)
# Bill
self.custName = StringVar(None)
location_ent =Entry(self.radioframe,textvariable = self.custName)
# Bill
location_ent.grid(row =2, column = 2, sticky = W,columnspan=2)
location_ent.update()
location_ent.focus_set()

def logDetails(self):
self.logframe = LabelFrame(self,text="Log Details",height= 520,width =390,padx=15)
self.logframe.grid_propagate(0)

self.logframe.grid_rowconfigure(0,weight =1)
self.logframe.grid_columnconfigure(0,weight=1)

xscrollbar = Scrollbar(self.logframe,orient = HORIZONTAL)
xscrollbar.grid(row=1, column=1, sticky=E+W,columnspan=2)

yscrollbar = Scrollbar(self.logframe,orient= VERTICAL)
yscrollbar.grid(row=0, column=3, sticky=N+S)

text = Text(self.logframe,width=50,height=50, wrap=NONE, xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
text.grid(row=0, column=1, columnspan=2)
# attach listbox to scrollbar
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)

button_1 = Button(self.logframe, text="View", command=printMessage,width =10)
button_1.grid(row=2,column= 1)

button_2 = Button(self.logframe, text="Export", command=printMessage,width =10)
button_2.grid(row=2,column= 2)

self.logframe.grid(row=0,column =1,rowspan=5)

def otherFunctionInterface(self):
self.otherFrame = LabelFrame(self,text="Other Function",height= 400,width =300)
self.otherFrame.grid(row=4, column=0)
self.otherFrame.grid_propagate(0)

OpenPreviousCaseFile = Button(self.otherFrame, text="Open previous case file", command=printMessage,height = 4,
width =21)
OpenPreviousCaseFile.grid(row=5,column= 0,pady=5,padx=5)

OpenPreviousTracingResult = Button(self.otherFrame, text="Open last tracing result ", command=printMessage,
height = 4, width =21)
OpenPreviousTracingResult.grid(row=6,column= 0,pady=5,padx=5)

OpenMenualbtn = Button(self.otherFrame, text="User manual", command=printMessage,height =4, width =21)
OpenMenualbtn.grid(row=7,column= 0,pady=5,padx=5)

AboutBtn = Button(self.otherFrame, text="About", command=printMessage,height = 4, width =21)
AboutBtn.grid(row=8,column= 0,pady=5,padx=5)

locateCaseFile = Entry(self.otherFrame)
locateCaseFile.grid(row=5,column = 1)

locateTraceResult = Entry(self.otherFrame)
locateTraceResult.grid(row=6,column = 1)
def open(self):
filename = filedialog.askopenfilename(parent=root,title='Choose a file')
# Bill
self.custName.set(filename)


def printMessage():
print("Wow this actually worked!")

if __name__=='__main__':
root=Tk()
root.title("Windows User Activity History Tracing and Analysing System")
tif= TracingInterface(root)
root.mainloop()

演示

下面是运行上面程序的截图:

enter image description here

关于Python:打开文件对话框没有出现,因为无法访问文本变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38430257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com