目前正在学习 tkinter 并且已经走到了尽头。每次我单击 GUI 中的一个按钮(在登录屏幕中输入“用户名”和“密码”后)都会创建并出现一个新的子窗口。正如它应该。我现在想要做的是确保只创建一个窗口,并且任何后续的点击都不会创建更多的窗口。这怎么可能?
from tkinter import *
class Main():
def __init__(self, master):
self.master = master
self.master.title("Main Window")
self.button1 = Button(self.master, text="Click Me 1", command = self.Open1)
self.button1.grid(row=0, column=0, sticky=W)
self.button2 = Button(self.master, text="Click Me 2", command = self.Open2)
self.button2.grid(row=0, column=2, sticky=W)
self.button3 = Button(self.master, text="Close", command = self.Close)
self.button3.grid(row=1, column=0, sticky=W)
def Login(self):
login_win = Toplevel(self.master)
login_window = Login(login_win)
login_window.Focus()
#main_window.Hide()
def Open1(self):
second_window = Toplevel(self.master)
window2 = Second(second_window)
def Open2(self):
third_window = Toplevel(self.master)
window3 = Third(third_window)
def …
Run Code Online (Sandbox Code Playgroud) 下面是一个程序,要求用户输入食谱并将其成分存储在一组列表中.然后程序将列表数据存储到文本文件中.如果选择选项2,它将从文本文件中检索存储的数据并将其加载回程序以进行处理并显示给用户.
文本文件不需要以人类可读的格式存储数据,但在检索之后,它必须采用可以识别每个列表项并且数量值需要能够进行计算的格式.
我的方法是将列表轻松转储到文本文档中.检索数据时,首先将每行添加到变量中,删除方括号,语音标记等,然后将其拆分回列表.
这些似乎是一种相当漫长而且效率低下的方式.当然有一种更简单的方法将列表数据存储到文件中,然后直接检索到列表中?
那么,有更简单有效的方法吗?或者,是否有另一种方法再次更简单/有效?
while True:
print("1: Enter a Recipe")
print("2: Calculate Your Quantities")
option = input()
option = int(option)
if option == 1:
name = input("What is the name of your meal?: ")
numing = input("How many ingredients are there in this recipe?: ")
numing = int(numing)
orignumpep = input("How many people is this recipe for?: ")
ingredient=[]
quantity=[]
units=[]
for x in range (0,numing):
ingr = input("Type in your ingredient: ")
ingredient.append(ingr)
quant = input("Type in the …
Run Code Online (Sandbox Code Playgroud) http://localhost/testsite1/coder2/?id=71
我有带有参数的网址id=71
。
由于我正在 WordPress 中进行开发,因此我无法使用该参数$_GET['id']
,因此需要使用它get_query_var('id')
来访问该参数。
然而,这并没有产生任何结果。
当我通过运行代码查看所有变量时:
global $wp_query;
var_dump($wp_query->query_vars);
Run Code Online (Sandbox Code Playgroud)
...我在任何地方都看不到该id
参数。
C:\wamp64\www\testsite1\wp-content\plugins\CSUKCODER\csukcoder.php:92:
array (size=64)
'page' => int 0
'pagename' => string 'coder2' (length=6)
'error' => string '' (length=0)
'm' => string '' (length=0)
'p' => int 0
'post_parent' => string '' (length=0)
'subpost' => string '' (length=0)
'subpost_id' => string '' (length=0)
'attachment' => string '' (length=0)
'attachment_id' => int 0
'name' => string 'coder2' (length=6)
'page_id' => int 0
'second' => …
Run Code Online (Sandbox Code Playgroud) php wordpress wordpress-theming custom-wordpress-pages query-variables
当收到来自 OpenAI 模型的响应时text-davinci-003
,我能够使用以下 PHP 代码从响应中提取文本:
$response = $response->choices[0]->text;
Run Code Online (Sandbox Code Playgroud)
这是text-davinci-003
响应代码:
{
"id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
"object": "text_completion",
"created": 1589478378,
"model": "text-davinci-003",
"choices": [
{
"text": "\n\nThis is indeed a test",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 7,
"total_tokens": 12
}
}
Run Code Online (Sandbox Code Playgroud)
我现在尝试更改我的代码以使用最近发布的gpt-3.5-turbo
模型,该模型返回的响应略有不同:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?",
},
"finish_reason": "stop"
}], …
Run Code Online (Sandbox Code Playgroud) 我正在尝试对新发布的模型执行 API 调用gpt-3.5-turbo
,并具有以下代码,该代码$query
应向 API 发送查询(通过变量),然后从 API 中提取响应消息的内容。
但我每次通话都收到空响应。有什么想法我做错了什么吗?
$ch = curl_init();
$query = "What is the capital city of England?";
$url = 'https://api.openai.com/v1/chat/completions';
$api_key = 'sk-**************************************';
$post_fields = [
"model" => "gpt-3.5-turbo",
"messages" => ["role" => "user","content" => $query],
"max_tokens" => 500,
"temperature" => 0.8
];
$header = [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if …
Run Code Online (Sandbox Code Playgroud) 像这样转换数组的最有效方法是什么:
[ [ 'Quiz 1' , 89 ] , ['Quiz 2' , 78] , ['Quiz 1' , 56] , ['Quiz 1' , 25] , ['Quiz 2' , 87] , ['Quiz 3' , 91] ]
Run Code Online (Sandbox Code Playgroud)
对此:
[ [ 'Quiz 1' , 89, 56, 25] , ['Quiz 2' , 78, 87] , ['Quiz 3' , 91] ]
Run Code Online (Sandbox Code Playgroud)
在 PHP 中?