我正在开发一个程序,在其中单击一个文本“打开”的按钮,它将使用filedialog.askopenfilename
打开一个文件选择窗口,但该按钮不会出现,并且它将自动调出窗口而不需要按下按钮。这是代码:
from tkinter import *
from tkinter import filedialog
root = Tk()
root.title("Snake converter")
sim = filedialog.askopenfilename(filetypes = (("Snake files", "*.sim"),("Python Files", "*.py"),("All files", "*.*")))
openbutton = Button(root, text = "Open", width = 10, command = sim)
答案 0 :(得分:3)
您的代码中没有任何地方您正在调用几何管理器(pack
,grid
,place
等),因此您的按钮不会显示。即使将filedialog.askopenfilename
分配给对象,def sim():
filedialog.askopenfilename(filetypes = (("Snake files", "*.sim"),("Python Files", "*.py"),("All files", "*.*")))
openbutton = Button(root, text = "Open", width = 10, command=sim)
openbutton.pack()
root.mainloop()
也会立即运行。我也不确定你可以将对象称为按钮功能。请尝试以下方法:
from tkinter import *
from tkinter import filedialog
root = Tk()
root.title("Snake converter")
def sim():
filedialog.askopenfilename(filetypes = (("Snake files", "*.sim"),("Python Files", "*.py"),("All files", "*.*")))
openbutton = Button(root, text = "Open", width = 10, command=sim)
openbutton.pack()
root.mainloop()
您的代码也应如下所示:
var relX = event.chart.mouseX;
var relY = event.chart.mouseY;
我还会检查frequent questions作为tkinter的新手。