[英]Python Tkinter access variable inside function for use outside of function
I'm building a gui to get an excel file and calendar input from a user.我正在构建一个 gui 以从用户那里获取 excel 文件和日历输入。 I want to use the file path and calendar date outside the functions in which they are called.
我想在调用它们的函数之外使用文件路径和日历日期。 How might I accomplish this?
我怎样才能做到这一点? I tried using global variables inside these functions but to no avail.
我尝试在这些函数中使用全局变量但无济于事。
import tkinter as tk
from tkinter import *, filedialog
from tkcalendar import Calendar, DateEntry
import pandas as pd
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
root = Tk()
def browse():
root.filename = filedialog.askopenfile(initialdir="/downloads", title="Select a file",
filetypes=((".xlsx files", "*.xlsx"), ("all files", "*.*")))
def date_picker():
top = tk.Toplevel(root)
cal = Calendar(top, font="Arial 14", selectmode='day', locale='en_US', disabledforeground='red',
cursor="hand1", date_pattern='MM/dd/yyyy')
cal.pack(fill="both", expand=True)
def quit():
top.destroy()
tk.Button(top, text="ok", command=quit).pack()
def exit():
root.destroy()
okButton = Button(root, text="OK", command=exit)
stuLabel = Label(root, text="Date")
calLabel = Label(root, text="Date to Start Summing")
calButton = Button(root, text="Date", command=date_picker)
fileButton = Button(root, text="File", command=browse)
fileLabel = Label(root, text="Select a File")
exitButton = Button(root, text="Exit")
stuLabel.grid(row=0, column=0)
calLabel.grid(row=1, column=0)
fileLabel.grid(row=2, column=0)
calButton.grid(row=1, column=1)
fileButton.grid(row=2, column=1)
okButton.grid(row=3, column=0)
exitButton.grid(row=3, column=1)
root.mainloop()
browser = webdriver.Chrome()
browser.get('a website')
type(browser)
delay = 20
df = pd.read_excel(filename, Sheet_name=0, header=None) # Here I want to access the chosen file
sum_start = browser.find_element_by_name('date')
sum_start.click()
# clear date
sum_start.clear()
sum_start.send_keys(str(date) + Keys.ENTER) # Here I want to access the date selected by the user
There is difference between filedialog
calls: filedialog
调用之间存在差异:
askopenfile()
returns the opened file object in read-only mode, askopenfilename()
returns the selected filename. askopenfile()
以只读模式返回打开的文件 object, askopenfilename()
返回选择的文件名。
This may be why you are having difficulties with globals.这可能就是您在使用全局变量时遇到困难的原因。 With the filename as a string it works fine (for me...)
使用文件名作为字符串它工作正常(对我来说......)
from tkinter import *
from tkinter import filedialog
root = Tk()
def browse():
global filename
filename = filedialog.askopenfilename(
initialdir="/downloads", title="Select a file",
filetypes=((".xlsx files", "*.xlsx"), ("all files", "*.*")))
browseButton = Button(root, text='Browse', command=browse)
browseButton.pack(padx=40, pady=20)
root.mainloop()
print(filename) # Here I want to access the chosen file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.