按钮未出现在tkinter中。(python)

按钮未出现在tkinter中。(python),python,button,tkinter,Python,Button,Tkinter,我正在开发一个程序,在这个程序中,你点击一个按钮,按钮上有文本“打开”,它将使用filedialog.askopenfilename弹出一个文件选择窗口,但按钮不会出现,它将自动弹出窗口,而无需按下按钮。代码如下: from tkinter import * from tkinter import filedialog root = Tk() root.title("Snake converter") sim = filedialog.askopenfilename(filetypes = ((

我正在开发一个程序,在这个程序中,你点击一个按钮,按钮上有文本“打开”,它将使用
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)

在您的代码中,您没有调用几何体管理器(
pack
grid
place
等),因此您的按钮不会显示。此外,即使已将文件分配给对象,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()

作为tkinter的新手,我也要检查一下。

在您的代码中,您没有调用几何体管理器(
pack
grid
place
等),因此您的按钮不会显示。此外,即使已将文件分配给对象,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()

我也是tkinter的新来者。

谢谢!真不敢相信我竟然忘了!谢谢真不敢相信我竟然忘了!