Python, Tkinter, trying to pull random numbers from a list based off user input for number and have results open in mew window -

Title pretty much sums it up, pasting the script below.Anytime its run the following error is generat

Title pretty much sums it up, pasting the script below. Anytime its run the following error is generated. I am extremely new to python and tkinter. So you know supernoob! But I appreciate help on this and if it's something I can't find a post of in the forums I will happily ask the question and pray that I can get a solid answer and explanation about how the code should be. Tim the answer and example you provided worked perfectly. However I did have another concept that's tied into this. How would you modify the script to two input boxes that display on the same popup?

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.496.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 2068, in __call__
    return self.func(*args)
           ~~~~~~~~~^^^^^^^
  File "C:\Users\falso\Desktop\Development\Software\1.py", line 10, in generate_random_value
    result_label.config(text=random_values)
    ^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'result_label' where it is not associated with a value

Here is current script

import tkinter as tk
import random


def generate_random_value():
    try:
        num_values = int(entry.get())
        if num_values > 0 and num_values <= len(my_list):
            random_values = random.sample(my_list, num_values)
            result_label.config(text=random_values)
            new_window = Toplevel(window)
            new_window.title("Oops!!")
            result_label=Label(new_window, text='result')
            result_label.pack()
            new_window.mainloop()
        else:
            result_label.config(text="Invalid input")
            result_label.pack_fet()
    except ValueError:
        result_label.config(text="Invalid input")
           
window=tk.Tk()
window.title("Random Value Generator")

my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

entry = tk.Entry(window)
entry.pack()

button = tk.Button(window, text="Generate", command=generate_random_value)
button.pack()


result_label = tk.Label(window, text="")
result_label.pack()

window.mainloop()

Hit a dead end with zero results.  Tried a few manipulations and renaming with no luck.

Title pretty much sums it up, pasting the script below. Anytime its run the following error is generated. I am extremely new to python and tkinter. So you know supernoob! But I appreciate help on this and if it's something I can't find a post of in the forums I will happily ask the question and pray that I can get a solid answer and explanation about how the code should be. Tim the answer and example you provided worked perfectly. However I did have another concept that's tied into this. How would you modify the script to two input boxes that display on the same popup?

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.496.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 2068, in __call__
    return self.func(*args)
           ~~~~~~~~~^^^^^^^
  File "C:\Users\falso\Desktop\Development\Software\1.py", line 10, in generate_random_value
    result_label.config(text=random_values)
    ^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'result_label' where it is not associated with a value

Here is current script

import tkinter as tk
import random


def generate_random_value():
    try:
        num_values = int(entry.get())
        if num_values > 0 and num_values <= len(my_list):
            random_values = random.sample(my_list, num_values)
            result_label.config(text=random_values)
            new_window = Toplevel(window)
            new_window.title("Oops!!")
            result_label=Label(new_window, text='result')
            result_label.pack()
            new_window.mainloop()
        else:
            result_label.config(text="Invalid input")
            result_label.pack_fet()
    except ValueError:
        result_label.config(text="Invalid input")
           
window=tk.Tk()
window.title("Random Value Generator")

my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

entry = tk.Entry(window)
entry.pack()

button = tk.Button(window, text="Generate", command=generate_random_value)
button.pack()


result_label = tk.Label(window, text="")
result_label.pack()

window.mainloop()

Hit a dead end with zero results.  Tried a few manipulations and renaming with no luck.
Share Improve this question edited Feb 2 at 20:17 Vampire 38.7k4 gold badges81 silver badges106 bronze badges asked Feb 1 at 6:10 Bear1981Bear1981 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your main issue is that you are reusing the name "result_label", which breaks the connection with your global. And you don't need to create a second mainloop. You need exactly one mainloop call in any given program.

This seems to do what you want:

import tkinter as tk
import random

def generate_random_value():
    try:
        num_values = int(entry.get())
        if num_values > 0 and num_values <= len(my_list):
            random_values = random.sample(my_list, num_values)
            new_window = tk.Toplevel(window)
            new_window.title("Wow!!")
            xresult_label=tk.Label(new_window, text='result')
            xresult_label.pack()
            xresult_label.config(text=random_values)
        else:
            result_label.config(text="Invalid input")
            result_label.pack_fet()
    except ValueError:
        result_label.config(text="Invalid input")
           
window=tk.Tk()
window.title("Random Value Generator")

my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

entry = tk.Entry(window)
entry.pack()

button = tk.Button(window, text="Generate", command=generate_random_value)
button.pack()

result_label = tk.Label(window, text="")
result_label.pack()

window.mainloop()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741884881a4371565.html

相关推荐

  • PackML over OPC UA

    在当今数字化转型的浪潮中,制造业正面临着前所未有的挑战与机遇。如何实现设备之间的高效通信与集成,成为提升生产效率、降低成本的关键。OPC UA(OPC Unified Architecture)与PackML(Packaging Machi

    1小时前
    00
  • 1.8w字图解Java并发容器: CHM、ConcurrentLinkedQueue、7 种阻塞队列的使用场景和原理

    文章多图且内容硬核,建议大家收藏上一章《1.6w 字图解 Java 并发:多线程挑战、线程状态和通信、死锁;AQS、ReentrantLock、Condition 使用和原理》,我们开启了 Java 高并发系列的学习,透彻理解 Java 并

    1小时前
    00
  • 10个 DeepSeek 神级提示词,建议收藏!

    在当下人工智能飞速发展的时代,DeepSeek 作为一款功能强大的 AI 工具,能够帮助我们实现各种创意和需求。然而,要充分发挥它的潜力,掌握一些巧妙的提示词至关重要。今天,就为大家精心整理了 15 个 DeepSeek 神级提示词,涵盖多

    1小时前
    00
  • HLS最全知识库

    HLS最全知识库副标题-FPGA高层次综合HLS(二)-Vitis HLS知识库高层次综合(High-level Synthesis)简称HLS,指的是将高层次语言描述的逻辑结构,自动转换成低抽象级语言描述的电路模型的过程。对于AMD Xi

    1小时前
    00
  • 我用AI监控了奥特曼,当他一发推特AI就会自动给我打电话。

    上周我真的扛不住了。奥特曼这个孙贼,发了个X说,“要发一个礼拜的好东西”。我信了他的邪,明明出差1周,每天早上9点不到就要起来参加活动,但是晚上根本不敢睡觉,天天蹲到凌晨3点半,蹲到他们那边时间中午12点多,我才敢去睡觉。真的,那一整周,我

    1小时前
    00
  • 非nvidia卡torchvision报错修复: operator torchvision::nms does not exist

    在Ascend 910b上安装vllm, 会自动把torchaudio和torchvision安装上去.安装前代码语言:shell复制pip list | grep torchtorch

    59分钟前
    00
  • 电脑开机会默认一件GHOST

    关于电脑开机会自己重装系统 前段时间电脑一开机就遇到会自己ghost的问题&#xff0c;而且一直再重复同样的操作&#xff0c;我点击restart的时候到开启页面又会自动ghost&#xff0c;而且此页面停留

    56分钟前
    00
  • 国产车载通信测试方案:车规级CAN SIC芯片测试技术解析

    随着智能网联汽车的快速发展,车辆内部电子控制单元(ECU)数量激增,动力总成、高级驾驶辅助系统(ADAS)、车身控制等功能对车载通信网络的稳定性与速率提出了更高要求。传统CAN FD总线在复杂拓扑中面临信号振铃、通信速率受限(实际速率通常低

    52分钟前
    00
  • Power BI 无公式实现帕累托图表

    帕累托分析(Pareto Analysis),也被称为8020法则、关键少数法则,是一种常用的管理工具,用于识别和处理影响业务的主要因素。看到李伟坚老师在Excel使用Vega实现了花式帕累托(参考:Excel 零公式实现高级帕累托图表)

    43分钟前
    00
  • 3、win10重装系统后Mysql环境和数据的恢复

    因为电脑是机哥的原因&#xff0c;重装了好几次电脑&#xff0c;因为我习惯把软件都装在D盘。所以很多东西都还比较好恢复&#xff0c;在网上学会了怎么不卸载重装数据库&#xff0c;自己记录以备后面自己查

    39分钟前
    00
  • CUT&amp;amp;Tag 数据处理和分析教程(7)

    过滤某些项目可能需要对比对质量分数进行更严格的过滤。本文细讨论了bowtie如何分配质量分数,并举例说明。MAPQ(x) = -10 * log10log10(P(x is mapped wrongly)) = -10 * log10(p)

    36分钟前
    10
  • 重装系统只影响C盘吗?深入解析系统重装的全过程

    重装系统只影响C盘吗?深入解析系统重装的全过程 在计算机的日常使用中,重装系统是一个常见的操作,尤其是在系统出现故障、感染病毒或需要优化系统性能时。然而,许多用户对于重装系统的具体过程和影响存在误解,认为重装系统仅仅是对C盘进行清空和重置

    27分钟前
    10
  • 大模型驱动金融数据应用的实战探索

    近年来,人工智能技术的飞速发展正在重塑全球各行各业的生态格局,金融行业作为数据密集型领域,更是首当其冲。大模型凭借其强大的自然语言处理、逻辑推理和生成能力,逐渐成为金融数据应用的核心驱动力。本文将从行业背景与趋势、核心场景重构、产品能力提升

    25分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要&#xff1a;在安装Windows Server 20192022后&#xff0c;会出现系统版本为 Evaluation 评估版情况&#xff0c;如提示Windows许可证已到期&#xff0c;就

    20分钟前
    00
  • 人工智能与ai有什么区别

    一、引言:概念之辨的必要性在科技浪潮席卷全球的当下,人工智能(Artificial Intelligence,简称AI)已成为人们耳熟能详的词汇。然而,当我们深入探讨时,会发现“人工智能”与“AI”这两个表述在语义和使用场景上存在微妙差异。

    20分钟前
    00
  • 实现一个 MySQL 配置对比脚本需要考虑哪些细节?

    作者:李彬,爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查。爱好有亿点点多,吉他、旅行、打游戏爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。本文约 1500 字,预计阅读需要 3 分钟。引言想

    19分钟前
    00
  • 在VMware虚拟机中安装Windows 7全攻略(避坑指南)

    ⚠️写在前面 最近发现很多开发者在调试老旧系统时都需要用到Windows 7环境&#xff08;特别是银行、医疗等行业的遗留系统&#xff09;&#xff0c;但实体机安装既不现实也不安全。今天就手把手教你在虚拟机

    19分钟前
    00
  • 人工智能适合什么人学

    一、引言:人工智能浪潮下的新机遇在当今科技飞速发展的时代,人工智能(AI)无疑是最为耀眼的技术明星之一。从智能语音助手到自动驾驶汽车,从医疗诊断辅助到金融风险预测,人工智能正以前所未有的速度改变着我们的生活和工作方式。随着全球领先的终身学习

    14分钟前
    00
  • Windows系统密钥检测工具PIDKey 2.1中文版

    Windows系统密钥检测工具PIDKey 2.1中文版 【下载地址】Windows系统密钥检测工具PIDKey2.1中文版 Windows系统密钥检测工具PIDKey 2.1中文版是一款功能强大的工具&#xff0c;专为管理Win

    14分钟前
    00
  • 人工智能应用领域有哪些

    人工智能应用领域有哪些一、引言随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面,成为推动社会进步的重要力量。从医疗健康到金融服务,从教育学习到智能制造,人工智能以其独特的技术优势,为各行各业带来了前所未有的变革。本文旨在

    6分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信