我希望能夠點擊html中的按鈕並調用python函數。 我試過this,它可以工作,但只適用於文本。我已經看到here,你可以使用按鈕的功能名稱的行動,但它不工作,我不知道爲什麼:/用按鈕調用python函數
而我不想要點擊按鈕轉到另一個頁面後,我想留在同一頁面上,只需執行函數中的代碼即可。
我的PY文件:
from flask import Flask
from flask import render_template
import tkinter as tk
from tkinter import filedialog
import sys
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('hello.html')
@app.route('/upload/')
def uploaduj():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
return file_path
我的html文件:
<!doctype html>
<title>Flaskr</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<h1>Flaskr</h1>
<div class=metanav>
<button action="/upload/">Klik</button>
</div>
我真的很新的Python和瓶所以每一個幫助表示讚賞。
編輯:我現在知道的Tkinter將不會在Web瀏覽器上
你不能打開窗戶Tkinter的在瀏覽器中,你能嗎? –
我可以,那部分作品完美 – kemis
真的嗎?它看起來像你讓服務器打開一個文件對話框。這應該不會工作,如果服務器在另一臺計算機上 –