GVKun编程网logo

Python-为什么Tkinter Entry的get函数什么都不返回?(python tkinter get)

86

以上就是给各位分享Python-为什么TkinterEntry的get函数什么都不返回?,其中也会对pythontkinterget进行解释,同时本文还将给你拓展Ajax请求什么都不返回。为什么?、A

以上就是给各位分享Python-为什么Tkinter Entry的get函数什么都不返回?,其中也会对python tkinter get进行解释,同时本文还将给你拓展Ajax请求什么都不返回。为什么?、Ajax请求什么都不返回为什么?、php – 无法使用file_get_contents(),什么都不返回、Python -在Tkinter中交互式验证Entry小部件内容等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Python-为什么Tkinter Entry的get函数什么都不返回?(python tkinter get)

Python-为什么Tkinter Entry的get函数什么都不返回?(python tkinter get)

我正在尝试使用一个Entry字段来获取手动输入,然后使用该数据。

我发现的所有资料都声称我应该使用该get()函数,但是我还没有找到一个简单的可运行的迷你示例,因此无法使用它。

我希望有人可以给我打电话,告诉我我做错了什么。这是一个迷你文件:

from tkinter import *master = Tk()Label(master, text="Input: ").grid(row=0, sticky=W)entry = Entry(master)entry.grid(row=0, column=1)content = entry.get()print(content)  # does not workmainloop()

这给了我一个Entry我可以输入的字段,但是一旦输入数据我就什么也不能做。

我怀疑我的代码不起作用,因为最初它entry是空的。但是,一旦输入了输入数据,我该如何访问呢?

答案1

小编典典

看起来你可能对何时运行命令感到困惑。在你的示例中,你是get在GUI有机会在屏幕上显示之前调用该方法的(在调用之后发生)mainloop

尝试添加一个调用该get方法的按钮。如果你将应用程序编写为类,这会容易得多。例如:

import tkinter as tkclass SampleApp(tk.Tk):    def __init__(self):        tk.Tk.__init__(self)        self.entry = tk.Entry(self)        self.button = tk.Button(self, text="Get", command=self.on_button)        self.button.pack()        self.entry.pack()    def on_button(self):        print(self.entry.get())app = SampleApp()app.mainloop()

运行程序,输入条目小部件,然后单击按钮。

Ajax请求什么都不返回。为什么?

Ajax请求什么都不返回。为什么?

以下是ajax请求。

$.post('delete.php',{'deletearray':deletearray,'dir':dir},function(deleted,undeleted){
    if(undeleted == 0) {
        alert('All ' + deleted + ' files delted from the server');
    } else {
        alert(deleted + ' files deleted and ' + undeleted + ' files could not be deleted');
    }
},'json');

这就是delete.php

<?php
    if(isset($_POST['deletearray'])) {
        $files = $_POST['deletearray'];
        $dir = $_POST['dir'];
        $deleted = 0;
        $undeleted = 0;

        foreach($files as $file) {
            if(unlink($dir.$file) && unlink($dir.'thumb/'.$file)) {
                $deleted ++;
            } else {
                $undeleted ++;
            }
        }
        echo json_encode($deleted,$undeleted);
    }
    return;
?>

运行代码后,它将成功删除文件,但不会显示任何消息。

我也尝试将ajax请求更改为:

 $.post('delete.php',{deletearray:deletearray,dir:dir},undeleted){
    alert("php finished");
 },'json');

仍然不显示该消息。所以我想在delete.php文件中出了点问题。请帮忙。

Ajax请求什么都不返回为什么?

Ajax请求什么都不返回为什么?

以下是ajax请求。

$.post('delete.php',{'deletearray':deletearray,'dir':dir},function(deleted,undeleted){
    if(undeleted == 0) {
        alert('All ' + deleted + ' files delted from the server');
    } else {
        alert(deleted + ' files deleted and ' + undeleted + ' files could not be deleted');
    }
},'json');

这就是delete.php

<?php
    if(isset($_POST['deletearray'])) {
        $files = $_POST['deletearray'];
        $dir = $_POST['dir'];
        $deleted = 0;
        $undeleted = 0;

        foreach($files as $file) {
            if(unlink($dir.$file) && unlink($dir.'thumb/'.$file)) {
                $deleted ++;
            } else {
                $undeleted ++;
            }
        }
        echo json_encode($deleted,$undeleted);
    }
    return;
?>

运行代码后,它将成功删除文件,但不会显示任何消息。

我也尝试将ajax请求更改为:

 $.post('delete.php',{deletearray:deletearray,dir:dir},undeleted){
    alert("php finished");
 },'json');

仍然不显示该消息。所以我想在delete.php文件中出了点问题。请帮忙。

php – 无法使用file_get_contents(),什么都不返回

php – 无法使用file_get_contents(),什么都不返回

我正在尝试使用此代码从不属于我的网站获取一些数据.

<?
$text = file_get_contents("https://ninjacourses.com/explore/4/");
echo $text;
?>

但是,没有任何回声,字符串长度为0.

我之前已经完成了这个方法,它没有任何问题,但是对于这个网站,它根本不起作用.

谢谢!

解决方法:

我设法使用curl获取内容,如下所示:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://ninjacourses.com/explore/4/");
$result = curl_exec($ch);
curl_close($ch);

Python -在Tkinter中交互式验证Entry小部件内容

Python -在Tkinter中交互式验证Entry小部件内容

如何解决Python -在Tkinter中交互式验证Entry小部件内容?

正确的答案是,使用validatecommand小部件的属性。不幸的是,尽管在Tkinter中有足够的文档记录,但该功能在Tkinter领域中的记录不足。即使没有很好地记录下来,它也具有验证所需的一切,而无需求助于绑定或跟踪变量,或在验证过程中修改小部件。

诀窍是要知道您可以让Tkinter将特殊值传递给validate命令。这些值为您提供了决定数据是否有效所需的所有信息:编辑之前的值,编辑之后的值(如果编辑有效)以及其他几位信息。但是,要使用这些命令,您需要做一点巫术,以使此信息传递到您的validate命令。

注意:验证命令返回True或至关重要False。任何其他情况都会导致该小部件的验证被关闭。

这是一个仅允许小写的示例(并显示所有这些时髦的值):

import tkinter as tk  # python 3.x
# import Tkinter as tk # python 2.x

class Example(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        # valid percent substitutions (from the Tk entry man page)
        # note: you only have to register the ones you need; this
        # example registers them all for illustrative purposes
        #
        # %d = Type of action (1=insert, 0=delete, -1 for others)
        # %i = index of char string to be inserted/deleted, or -1
        # %P = value of the entry if the edit is allowed
        # %s = value of entry prior to editing
        # %s = the text string being inserted or deleted, if any
        # %v = the type of validation that is currently set
        # %V = the type of validation that triggered the callback
        #      (key, focusin, focusout, forced)
        # %W = the tk name of the widget

        vcmd = (self.register(self.onValidate),
                ''%d'', ''%i'', ''%P'', ''%s'', ''%s'', ''%v'', ''%V'', ''%W'')
        self.entry = tk.Entry(self, validate="key", validatecommand=vcmd)
        self.text = tk.Text(self, height=10, width=40)
        self.entry.pack(side="top", fill="x")
        self.text.pack(side="bottom", fill="both", expand=True)

    def onValidate(self, d, i, P, s, S, v, V, W):
        self.text.delete("1.0", "end")
        self.text.insert("end","OnValidate:\n")
        self.text.insert("end","d=''%s''\n" % d)
        self.text.insert("end","i=''%s''\n" % i)
        self.text.insert("end","P=''%s''\n" % P)
        self.text.insert("end","s=''%s''\n" % s)
        self.text.insert("end","S=''%s''\n" % S)
        self.text.insert("end","v=''%s''\n" % v)
        self.text.insert("end","V=''%s''\n" % V)
        self.text.insert("end","W=''%s''\n" % W)

        # disallow anything but lowercase letters
        if S == S.lower():
            return True
        else:
            self.bell()
            return False

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

有关调用该register方法时幕后情况的更多信息,请参见输入验证tkinter

解决方法

交互验证tkinter Entry小部件中的内容的推荐技术是什么?

我已经阅读了有关使用validate=True和的文章validatecommand=command,并且看来这些功能受到以下事实的限制:如果validatecommand命令更新了Entry小部件的值,这些功能将被清除。

鉴于这种行为,我们应该绑定的KeyPress,Cut以及Paste事件和监视/更新我们的Entry小部件的价值,通过这件事情?(还有我可能错过的其他相关事件?)

还是我们应该完全忘记交互式验证,而只对FocusOut事件进行验证?

关于Python-为什么Tkinter Entry的get函数什么都不返回?python tkinter get的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Ajax请求什么都不返回。为什么?、Ajax请求什么都不返回为什么?、php – 无法使用file_get_contents(),什么都不返回、Python -在Tkinter中交互式验证Entry小部件内容等相关知识的信息别忘了在本站进行查找喔。

本文标签: