鱼C论坛

 找回密码
 立即注册
查看: 1616|回复: 1

想获取tkinter中的toplevel中控件

[复制链接]
发表于 2022-5-24 17:58:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想引用子窗口root_01 中输出框e_grade,但是在函数tttttttt里面引用不到,求助求助啊

报错:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\panXingxing\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "F:/python/lianxi/treeview.py", line 64, in tttttttt
    print(e_grade)
NameError: name 'e_grade' is not defined




代码如下:
import tkinter.ttk
import tkinter
from tkinter import *
from tkinter import ttk

root = tkinter.Tk()
root.geometry('600x300')
mid_one = ['Morning1', 'Morning2', 'Morning3', 'Afternoon1', 'Afternoon2', 'Afternoon3']
mid_two = ['TwoMorning1', 'TwoMorning2', 'TwoMorning3', 'TwoAfternoon1', 'TwoAfternoon2', 'TwoAfternoon3']
mid_three = ['ThreeMorning11', 'ThreeMorning2', 'ThreeMorning3', 'ThreeAfternoon1', 'ThreeAfternoon2',
             'ThreeAfternoon3']

def get_combobox_num(key,mid):
    combobox_num = 0
    print('长度',len(mid))
    for i in range(len(mid)):
        if key == mid[i]:
            print(mid[i])
            combobox_num=i
    return combobox_num



def offff(*args):   #输出选中的树状表里的行内容
    values = table.item(table.selection(), 'values')
    root_01 = tkinter.Tk()
    root_01.geometry('400x200')
    e_grade = Entry(root_01, relief=SUNKEN, width=20, )  # 年级输入框
    e_grade.insert(0, values[3])
    e_grade.pack()
    if values[3] == '初一':
        print('初一')
        combox_class_type = ttk.Combobox(root_01, state='readonly', values=mid_one)
        combox_class_type.current(get_combobox_num(values[4],mid_one))
    elif values[3] == '初二':
        print('初二')
        combox_class_type = ttk.Combobox(root_01, state='readonly', values=mid_two)
        combox_class_type.current(get_combobox_num(values[4], mid_two))
    elif values[3] == '初三':
        print('初三')
        combox_class_type = ttk.Combobox(root_01, state='readonly', values=mid_three)
        combox_class_type.current(get_combobox_num(values[4], mid_three))
    combox_class_type.pack()
    e_name = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[5])  # 姓名输入框
    e_name.insert(0, values[5])
    e_name.pack()
    e_phone = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[6])  # 电话输入框
    e_phone.insert(0, values[6])
    e_phone.pack()
    e_pay = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[7])  # 缴费输入框
    e_pay.insert(0, values[7])
    e_pay.pack()
    e_remarks = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[9])  # 备注输入框
    e_remarks.insert(0, values[9])
    e_remarks.pack()
    b03 = Button(root_01, text='修改',command=tttttttt)
    b03.pack()
    root_01.mainloop()
    return

def tttttttt():
    print(e_grade)
    print('fffffff')
    return


columns = ['序号', '工作表','行数','年级','课程种类','姓名','电话','缴费','缴费时间','备注', ]

table = tkinter.ttk.Treeview(
    master=root,  # 父容器
    height=10,  # 表格显示的行数,height行
    columns=columns,  # 显示的列
    show='headings',  # 隐藏首列
)
table.heading(column='序号', text='序号')
table.heading(column='工作表', text='工作表')
table.heading(column='行数', text='行数')
table.heading(column='年级', text='年级')
table.heading(column='课程种类', text='课程种类')
table.heading(column='姓名', text='姓名')
table.heading(column='电话', text='电话')
table.heading(column='缴费', text='缴费')
table.heading(column='缴费时间', text='缴费时间')
table.heading(column='备注', text='备注')
table.column('序号',width=40,anchor='center')
table.column('工作表',width=50,anchor='center')
table.column('行数',width=40,anchor='center')
table.column('年级',width=50,anchor='center')
table.column('课程种类',width=100,anchor='center')
table.column('姓名',width=50,anchor='center')
table.column('电话',width=100,anchor='center')
table.column('缴费',width=40,anchor='center')
table.column('缴费时间',width=80,anchor='center')
table.column('备注',width=100,anchor='center')


neirong = [{'序号': '1' ,
                  '工作表':'ttttt',
                  '行数': '66',
                  '年级': '初三',
                  '课程种类': 'ThreeAfternoon3',
                  '姓名': '杜若龙',
                  '电话': '15865481536',
                  '缴费': '否',
                  '缴费时间':'22-5-12',
                  '备注':'黑师傅拉宋丹妮福利卡脚斗士了咖啡机阿萨德',},
           {'序号': '2',
            '工作表': 'ttttt',
            '行数': '44',
            '年级': '初一',
            '课程种类': 'Afternoon1',
            '姓名': '杜kad',
            '电话': '1586548188',
            '缴费': '否',
            '缴费时间': '22-5-20',
            '备注': '黑机阿萨德', }
           ]


table.insert(parent = "",index='end',values=tuple(neirong[0].values()))
table.insert(parent = "",index='end',values=tuple(neirong[1].values()))

table.bind('<Double-Button-1>',offff)


table.place(x=20,y=10)

root.mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-25 09:18:35 | 显示全部楼层
使用 global 声明

对你的代码修改如下(加了第28行):
  1. import tkinter.ttk
  2. import tkinter
  3. from tkinter import *
  4. from tkinter import ttk

  5. root = tkinter.Tk()
  6. root.geometry('600x300')
  7. mid_one = ['Morning1', 'Morning2', 'Morning3', 'Afternoon1', 'Afternoon2', 'Afternoon3']
  8. mid_two = ['TwoMorning1', 'TwoMorning2', 'TwoMorning3', 'TwoAfternoon1', 'TwoAfternoon2', 'TwoAfternoon3']
  9. mid_three = ['ThreeMorning11', 'ThreeMorning2', 'ThreeMorning3', 'ThreeAfternoon1', 'ThreeAfternoon2',
  10.              'ThreeAfternoon3']

  11. def get_combobox_num(key,mid):
  12.     combobox_num = 0
  13.     print('长度',len(mid))
  14.     for i in range(len(mid)):
  15.         if key == mid[i]:
  16.             print(mid[i])
  17.             combobox_num=i
  18.     return combobox_num



  19. def offff(*args):   #输出选中的树状表里的行内容
  20.     values = table.item(table.selection(), 'values')
  21.     root_01 = tkinter.Tk()
  22.     root_01.geometry('400x200')
  23.     global e_grade                                               # 加上这句
  24.     e_grade = Entry(root_01, relief=SUNKEN, width=20, )  # 年级输入框
  25.     e_grade.insert(0, values[3])
  26.     e_grade.pack()
  27.     if values[3] == '初一':
  28.         print('初一')
  29.         combox_class_type = ttk.Combobox(root_01, state='readonly', values=mid_one)
  30.         combox_class_type.current(get_combobox_num(values[4],mid_one))
  31.     elif values[3] == '初二':
  32.         print('初二')
  33.         combox_class_type = ttk.Combobox(root_01, state='readonly', values=mid_two)
  34.         combox_class_type.current(get_combobox_num(values[4], mid_two))
  35.     elif values[3] == '初三':
  36.         print('初三')
  37.         combox_class_type = ttk.Combobox(root_01, state='readonly', values=mid_three)
  38.         combox_class_type.current(get_combobox_num(values[4], mid_three))
  39.     combox_class_type.pack()
  40.     e_name = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[5])  # 姓名输入框
  41.     e_name.insert(0, values[5])
  42.     e_name.pack()
  43.     e_phone = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[6])  # 电话输入框
  44.     e_phone.insert(0, values[6])
  45.     e_phone.pack()
  46.     e_pay = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[7])  # 缴费输入框
  47.     e_pay.insert(0, values[7])
  48.     e_pay.pack()
  49.     e_remarks = Entry(root_01, relief=SUNKEN, width=20, textvariable=values[9])  # 备注输入框
  50.     e_remarks.insert(0, values[9])
  51.     e_remarks.pack()
  52.     b03 = Button(root_01, text='修改',command=tttttttt)
  53.     b03.pack()
  54.     root_01.mainloop()
  55.     return

  56. def tttttttt():
  57.     print(e_grade)
  58.     print('fffffff')
  59.     return


  60. columns = ['序号', '工作表','行数','年级','课程种类','姓名','电话','缴费','缴费时间','备注', ]

  61. table = tkinter.ttk.Treeview(
  62.     master=root,  # 父容器
  63.     height=10,  # 表格显示的行数,height行
  64.     columns=columns,  # 显示的列
  65.     show='headings',  # 隐藏首列
  66. )
  67. table.heading(column='序号', text='序号')
  68. table.heading(column='工作表', text='工作表')
  69. table.heading(column='行数', text='行数')
  70. table.heading(column='年级', text='年级')
  71. table.heading(column='课程种类', text='课程种类')
  72. table.heading(column='姓名', text='姓名')
  73. table.heading(column='电话', text='电话')
  74. table.heading(column='缴费', text='缴费')
  75. table.heading(column='缴费时间', text='缴费时间')
  76. table.heading(column='备注', text='备注')
  77. table.column('序号',width=40,anchor='center')
  78. table.column('工作表',width=50,anchor='center')
  79. table.column('行数',width=40,anchor='center')
  80. table.column('年级',width=50,anchor='center')
  81. table.column('课程种类',width=100,anchor='center')
  82. table.column('姓名',width=50,anchor='center')
  83. table.column('电话',width=100,anchor='center')
  84. table.column('缴费',width=40,anchor='center')
  85. table.column('缴费时间',width=80,anchor='center')
  86. table.column('备注',width=100,anchor='center')


  87. neirong = [{'序号': '1' ,
  88.                   '工作表':'ttttt',
  89.                   '行数': '66',
  90.                   '年级': '初三',
  91.                   '课程种类': 'ThreeAfternoon3',
  92.                   '姓名': '杜若龙',
  93.                   '电话': '15865481536',
  94.                   '缴费': '否',
  95.                   '缴费时间':'22-5-12',
  96.                   '备注':'黑师傅拉宋丹妮福利卡脚斗士了咖啡机阿萨德',},
  97.            {'序号': '2',
  98.             '工作表': 'ttttt',
  99.             '行数': '44',
  100.             '年级': '初一',
  101.             '课程种类': 'Afternoon1',
  102.             '姓名': '杜kad',
  103.             '电话': '1586548188',
  104.             '缴费': '否',
  105.             '缴费时间': '22-5-20',
  106.             '备注': '黑机阿萨德', }
  107.            ]


  108. table.insert(parent = "",index='end',values=tuple(neirong[0].values()))
  109. table.insert(parent = "",index='end',values=tuple(neirong[1].values()))

  110. table.bind('<Double-Button-1>',offff)


  111. table.place(x=20,y=10)

  112. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 03:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表