关键词:
我正在Python 3.7中使用Tkinter,作为一种让用户使用askopenfilename窗口选择文件的方法。我也有一个“运行”按钮。我想检查一下是否已打开文件。如果没有打开任何文件,我会收到一条错误消息,并且如果选择了文件,则希望该程序运行。
这里是我到目前为止的代码:
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
class GetInfo:
def __init__(self, master):
self.master = master
self.FileLabel = Label(master, text="Open the File to Use", font=("Arial Bold", 13)).grid(row=9, column=0,
sticky=W)
openFileCommand = master.register(self.FileOpen)
self.file_button = ttk.Button(master, text="Select File",
command=openFileCommand).grid(row=10, column=0, sticky=W, pady=10)
runCommand = master.register(self.getInput)
exitCommand = master.register(self.getCancel)
self.run_button = ttk.Button(master, text="Run",
command=runCommand).grid(row=20, column=0, sticky=W)
self.cancel_button = ttk.Button(master, text="Cancel",
command=exitCommand).grid(row=20, column=1, sticky=W)
def FileOpen(self):
self.File = filedialog.askopenfilename(title="Open the file",
filetypes=(("Files", "*.txt"), ("All Files", "*")))
self.file_only = self.File.split('/')[-1]
def getInput(self):
if self.File is None:
self.warning_window = tk.showerror('Error', 'Please select a file to use.')
else:
self.close_box_window = tk.messagebox.askokcancel('Running', "Running the program, default = 'ok'")
if self.close_box_window == True:
root.destroy()
else:
return
def getCancel(self):
self.MsgBox_window = tk.messagebox.askokcancel("Exit", "Are you sure you want to exit?", icon="warning",
default='cancel')
if self.MsgBox_window == False:
return
else:
root.destroy()
root = Tk()
root.geometry('800x500')
gui = GetInfo(root)
root.mainloop()
File = gui.file_only
我不断收到错误:
AttributeError: 'GetInfo' object has no attribute 'File'
我以前曾做过这项工作(不对self.File代码进行if / else检查)。我不确定如何获得想要的东西。任何帮助,将不胜感激。
class GetInfo:
def __init__(self, master):
self.File = None
此外,为避免一些粗心的错误,您可以使用if语句来判断函数FileOpen()
中的文件名:
def FileOpen(self):
filename = filedialog.askopenfilename(title="Open the file",
filetypes=(("Files", "*.txt"), ("All Files", "*")))
if filename: # when cancel, it will be ""
self.File = filename
self.file_only = self.File.split('/')[-1]
在pythontkinter中为事件创建日历视图[关闭](代码片段)
...我可以选择一天,看看我当天要做的事情,是时候使用了pythontkinter。目前在我的数据库中,我有日期,开始时间和结束时间的事件。请注意,我正在使用python3.任何人都可以给我一个线索,如何做到这一点,甚至发送一个链接到... 查看详情
如何在没有按钮单击pythontkinter的情况下进入另一个页面(代码片段)
所以,我正在使用tkinter在python中创建一个登录系统,我希望它在验证了电子邮件和密码后移动到另一个页面。我发现这样做的唯一方法是使用按钮单击命令。我只希望在验证电子邮件和密码后转到下一页。提前致谢。fromtkinterimp... 查看详情
pythontkinter的(代码片段)
pythontkinter的(代码片段)
python代理选择器,代理文件列表中的验证(代码片段)
pythontkinter上的多窗口(代码片段)
pythontkinter声音测试(.wav)(代码片段)
pythontkinter应用之用户登录界面(代码片段)
tkinter应用之用户登录界面实现效果程序代码用户的登录界面介乎无处不在,用户输入用户名,账号和密码后,系统进行验证,通过验证才可以进行后续的操作。一般而言,用户密码都是经过安全哈希算法和加... 查看详情
如何从pythontkinter应用程序中捕获任何输出到控制台?(代码片段)
我有一个大型的多文件,Tkinter应用程序,具有无数的功能,然后与Pyinstaller一起打包为可执行文件。它包含预期事件的错误处理。但是,它没有任何错误处理意外事件。在将应用程序打包为可执行文件之前运行应用程序时,会向... 查看详情
2021-08-23(代码片段)
python数据集中随机选择作为测试集、验证集一、功能从已有的数据集中随机选择出若干组数据作为测试集或者测试集。二、数据数据集由三种数据组成,分别是:前期数据(tif)、后期数据(tif)、标签图(png),所以数据集由三... 查看详情
pythontkinter控件与布局项目实战(代码片段)
代码部分:fromtkinterimport*importtkinter.messageboxasmessageboxclassTkdemo():def__init__(self):master=Tk()master.title("missWjz")master.geometry(‘800x800‘)#创建菜单栏menubar=Menu(master)master.config(menu=menu 查看详情
pythontkinter应用之抽奖程序(代码片段)
tkinter应用之抽奖程序实现效果程序代码使用tkinter实现抽奖程序,使用时可以修改抽奖名单,然后点击‘开始’和‘停止’按钮来控制界面上的名单滚动实现抽奖功能。当然该程序也可以用于上课时随机提问。实现效果程... 查看详情
pythontkinter尝试简化标签的事情(代码片段)
所以我试图让标签更简单,但我找不到任何方法来做到这一点ImtryingtomakeitlooklikethisbutwithjustoneLabelfromtkinterimport*fromdatetimeimportdateimportlibdetect_date=date.today()hari=detect_date.strftime("%A")myWindow=Tk()myWindow. 查看详情
pythontkinter应用之简易计算器(代码片段)
tkinter应用之简易计算器实现效果程序代码实现效果程序代码importreimporttkinterimporttkinter.messageboxroot=tkinter.Tk()#设置窗口的大小和位置root.geometry('300x270+400+100')#不允许改变窗口的大小root.resizable(False,False)#设置窗口标 查看详情
pythontkinter坐标转换(代码片段)
tkinter中坐标原点在左上角,横坐标向右,纵坐标向下,画图需要将坐标转换成右下角的某个点来符合我们的常用坐标坐标原点设为(x0,y0),横坐标向右,纵坐标向上,:转换:想实现坐标点(x,y)的显示 ... 查看详情
如何为标签列表制作“滚动条”?pythontkinter[复制](代码片段)
这个问题在这里已有答案: AddingascrollbartoagroupofwidgetsinTkinter2回答 这是我的代码:fromtkinterimport*fromPILimportImage,ImageTkim=Image.open(r"1asd.jpg")root=Tk()tkimage=ImageTk.PhotoImage( 查看详情
pythontkinter模块创建窗口v1.2(代码片段)
先上图代码如下1#-*-coding:utf-8-*-2importos3fromtkinterimport*45root=Tk()6root.title(‘执行窗口‘)78"""9V1.21011"""12defadd(a,b):13a=int(a)14b=int(b)15sum=eval(‘a+b‘)#执行表达式16print(‘a+b=‘,sum)1718returnsum1920#调用其他 查看详情
tic-tac-toe使用pythontkinter(代码片段)
使用pythontkinter的Tic-tac-toe游戏无法正常工作。Tic-tac-toe结构是正确的。我只是想改变点击事件。单击任意按钮时仅显示button9输出每次单击任何按钮,都会显示此输出fromtkinterimport*bclick=Truetk=Tk()tk.title("TicTactoe")tk.geometry("300x400")n=9bt... 查看详情