0
Follow
1
View

When Python runs for a long time, parts of the code in def are not executed, but the result can be displayed

dugu78 注册会员
2023-02-26 20:13

Run py while true for about ten minutes, same problem, so not tkinter

cxzakira 注册会员
2023-02-26 20:13

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > seems to be a Python program that defines a function print_label(trade_no, print_num), It takes two arguments: trade_no(a string) and print_num(an integer). The program imported several modules, including hashlib, urllib, time, datetime, jsonpath, openpyxl, requests, and win32com.client.
-
Inside the function, several variables are defined: url_path, query_time, data, and _sign. The function then uses the supplied data to send a POST request to the specified URL, using the requests module with the _app, _t, pay_time, end_time, limit, page, trade_status, and is_split parameters. The _sign parameter is evaluated using the hash_md5 function and is included in the data dictionary before the request is sent.
-
If the response status code is 200, the function uses the jsonpath module to extract data from the JSON response and assign it to the data_list. The function then walks through each element in the data_list and checks if the trade_no argument is a substring of the system order number(i.et(' system order number ')[0]). If so, append the res4 substring to the package_name list. If there is no element in the package_name list, the function sets the res['msg'] variable to 'This buyer has no combo item! '. If there are elements in the package_name list, the function sets the res['msg'] variable to 'print done! '.
-
Finally, the function loads the Excel workbook using the openpyxl module and walks through each worksheet in the workbook. If the worksheet name matches the string in the package_name list, the function prints the label. If the worksheet name does not match any of the strings in the package_name list, the function sets the res['id'] variable to 'the combination name is incorrect! '.

diamondth_king 注册会员
2023-02-26 20:13

That's why I wrote question 2. Look at

zhh1127 注册会员
2023-02-26 20:13

Problem 2 is not tkinter, it is pure python

cuiruxiang 注册会员
2023-02-26 20:13

What I said was that I was able to run all the time from the beginning, and that was the presumption, that the code was fine, except if I didn't close it down for ten minutes or so

du44022822 注册会员
2023-02-26 20:13

The problem may be caused by the tkinter mechanism, if the main thread is occupied by long calculations/loops, it will not be able to handle other events. Includes events that perform a button click when the user presses a key.

To solve this problem, long-running code can be placed in the background thread so that the main thread can continue to respond to events. You've implemented a function in your code called thread_it that takes a function as an argument and executes it in the background using threading.Thread.

So you can use this function to perform the get_all_labels function like this:

def click_button(print_num):
    global text
    thread_it(get_all_labels, print_num)
    text.insert(tk.END, f'本次打印了{trade_no_num}笔订单标签 \t{time.now().strftime("%Y-%m-%d %H:%M:%S")}\n')

This moves the get_all_labels function to the background thread to avoid taking too long on the main thread. In addition, thread-safe GUI operations must be used to ensure that code executed in the background thread updates the GUI correctly.

In tkinter, variable types such as tkinter.stringvar or tkinter.intvar can be used to store the values of GUI components and update their values in the background thread using the set() method.

For example, you can create a tkinter.StringVar object for the text box text and use the text.set() method in the click_button function to set the value of the text box.

def click_button(print_num):
    global text
    thread_it(get_all_labels, print_num)
    text.set(f'本次打印了{trade_no_num}笔订单标签 \t{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n')

This ensures that the value of the text box is updated in the background thread and avoids any long-running code execution in the main thread.

dongmrj 注册会员
2023-02-26 20:13

Code no problem, but when put up, delete some sensitive information, may delete the code is not complete. That doesn't affect my question.

dwkof520 注册会员
2023-02-26 20:13

Part of the answer refers to GPT, GPT_Pro is better for solving the problem
After running python for a long time, some of the code in def will not execute, but will display the result. It's usually because there's something wrong in the code, or something in the def that can't be done.

To solve this problem, we need to figure out what this code does. What does it do? Let's see if there are any errors in this code. Or an operation that cannot be performed?

  1. Let's first look at what this code does:
    import get_all_labels from import get_all_labels(), call get_all_labels() from click_button(), pass the print_num parameter, and insert the value of trade_no_num in text; Create thread t in function thread_it() and pass the click_button() function and argument 2 into t for execution; Call the thread_it() function in Print_all_Button.

  2. Let's see if there's anything wrong with this code:
    There's nothing between line 2 and line 3; The "=" sign is missing from the assignment statement for the trade_no_num variable in line 6; The "," sign is missing from the definition of the thread_it() function on line 9.

  3. There are also operations that cannot be performed:
    When we call the thread_it() function in Print_all_Button, we pass click_button() and 2, but the click_button() function needs the print_num argument to execute properly.

The modified code is as follows:

from 查询全部待打印标签 import get_all 1abels

def click_button(print_num):
    global text
    trade_no_num = get_all_labels(print_num=print_num)
    text.insert(tk.END,trade_no_num) 
    
def thread it(func,*args): 
    t = threading.Thread(target=func,args=args) 
    t.daemon = True 
    t.start() 
    
Print_al1_Button = tk.Button(root,text="",bg="",width=286,height-5,command=lambda: thread_it(click_button,2,print_num)) 
Print_all_Button.pack() 
text = tk.TextCroot 

If the answer is helpful, please accept it.

About the Author

Question Info

Publish Time
2023-02-26 20:13
Update Time
2023-02-26 20:13

Related Question

Sorbet:为proc0# call方法提供了太多参数

我如何运行C程序与输入在Netbeans

adobephotoshop -转换图像到位图使用脚本

请问raspberry pi os的任务栏有什么快捷键吗?

def函数为什么不能把字符变变成utf-8模式

如何增加块大小在request_toolbelt的MultipartEncoder?

使用case_when替换所有索引值

春季引导与IGoogleAuthenticator-如何自定义生成间隔的时间基于OTP码?

捕获kubectl集合命令在地球

如何使用信息.使用embed - Discord.js V13