hveagle 发表于 2024-2-9 17:42:52

求助

from tkinter import *
from math import *
from time import *

food = ['LV4 苹果(48)', 'LV4 苹果(48)', 'LV4 苹果(48)']
power = ['LV3 三jio尺', 'LV3 三jio尺', 'LV3 三jio尺', 'LV3 三jio尺', 'LV3 三jio尺']
full = 500
money = 666

root = Tk()
root.title('六六湖秘密探险 V2.0 Feb.6th 2024 -- 六六游戏城V8888.66.5.1.0.456.17.0a7')

w = Canvas(root, width=550, height=350)
w.pack()

v = IntVar()
v.set(1)

now = '家'

road = {'家':{'鱼C公园':, '食物超市1~6':}, \
      '鱼C公园':{'家':, '食物超市1~6':, '武器超市1~6':}, \
      '食物超市1~6':{'家':, '鱼C公园':, 'LV1打工仔公司':}, \
      '武器超市1~6':{'鱼C公园':, 'LV1打工仔公司':}, \
      'LV1打工仔公司':{'食物超市1~6':, '武器超市1~6':}}

def callback():
    print('啊!我被调用了')

def go2(button):
    if button == now:
      label3 = Label(w, text='你就在这儿')
      label3_window = Canvas.create_window(w, 540, 200, window=label3)
    else:
      w.delete(ALL)
      sleep(0.5)
      label4 = Label(w, text='Loading... 口口20%')
      label4_window = Canvas.create_window(w, 275, 340, anchor=W, window=label4)
      w.delete(ALL)
      sleep(0.5)
      label5 = Label(w, text='Loading... 口口口口40%')
      label5_window = Canvas.create_window(w, 275, 340, anchor=W, window=label4)
      w.delete(ALL)
      sleep(0.5)
      label6 = Label(w, text='【777警报】怪兽来袭!')
      label6_window = Canvas.create_window(w, 275, 340, window=label4)
      lpower = len(power)
      for i in range(lpower):
            Radiobutton(v, text=power, variable=v, value=i+1, indicatoron=False).pack(anchor=W)

position = {'A':((5, 5), (65, 65), 30, 'red', '家'), \
            'B':((70, 70), (130, 130), 30, 'orange', '鱼C公\n园'), \
            'C':((170, 70), (230, 130), 30, 'yellow', '食物超\n市1~6'), \
            'D':((70, 170), (130, 230), 30, 'green', '武器超\n市1~6'), \
            'E':((170, 270), (230, 330), 30, 'blue', 'LV1打\n工仔公\n司')}

def connect(x):
    a = position]
    b = position]
    a_center = ((a + a), (a + a))
    b_center = ((b + b), (b + b))
    if a_center < b_center and a_center < b_center:
      #右下I
      temp = (a_center, b_center)
      s1 = b_center - temp
      s2 = temp - a_center
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center + diff1, a_center + diff2, b_center - diff1, b_center - diff2
    elif a_center > b_center and a_center < b_center:
      #左下II
      temp = (a_center, b_center)
      s1 = temp - b_center
      s2 = temp - a_center
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center - diff1, a_center + diff2, b_center + diff1, b_center - diff2
    elif a_center > b_center and a_center > b_center:
      #左上III
      temp = (a_center, b_center)
      s1 = temp - b_center
      s2 = a_center - temp
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center - diff1, a_center - diff2, b_center + diff1, b_center + diff2
    elif a_center < b_center and a_center > b_center:
      #右上IV
      temp = (a_center, b_center)
      s1 = temp - b_center
      s2 = temp - a_center
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center + diff1, a_center - diff2, b_center - diff1, b_center + diff2
    elif a_center == b_center:
      #同行
      return a_center + 30, a_center, b_center - 30, b_center
    elif a_center == b_center:
      #同列
      return a_center, a_center + 30, b_center, b_center - 30

def go():
    w.delete(ALL)
    for i in list(position.keys()):
      w.create_oval(position, position, position, position, fill=position)
      center = (position + position, position + position)
      w.create_text(center, center, text=position)

    myline = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E'), ('D', 'E')]

    for each in myline:
      w.create_line(connect(each))

    the_label = Label(w, text='你想去哪儿?')
    the_label_window = Canvas.create_window(w, 540, 10, anchor=E, window=the_label)
    button_a = Button(w, text='家', command=go2('家'))
    button_a_window = Canvas.create_window(w, 540, 35, anchor=E, window=button_a)
    button_b = Button(w, text='鱼C公园', command=go2('鱼C公园'))
    button_b_window = Canvas.create_window(w, 540, 60, anchor=E, wind3ow=button_b)
    button_c = Button(w, text='食物超市1~6', command=go2('食物超市1~6'))
    button_c_window = Canvas.create_window(w, 540, 85, anchor=E, window=button_c)
    button_d = Button(w, text='武器超市1~6', command=go2('武器超市1~6'))
    button_d_window = Canvas.create_window(w, 540, 110, anchor=E, window=button_d)
    button_e = Button(w, text='LV1打工仔公司', command=go2('LV1打工仔公司'))
    button_e_window = Canvas.create_window(w, 540, 135, anchor=E, window=button_e)

label1 = Label(w, text='六六湖秘密探险 V2.0\nSixty-six Lake Secret Adventure')
label1_window = Canvas.create_window(w, 275, 125, window=label1)
label2 = Label(w, text='你要做什么?')
label2_window = Canvas.create_window(w, 275, 150, window=label2)
choose1 = Button(w, text='出发', command=go)
choose1_window = Canvas.create_window(w, 275, 175, window=choose1)
choose2 = Button(w, text='查看背包', command=callback)
choose1_window = Canvas.create_window(w, 275, 200, window=choose2)

mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/tkinter/__init__.py", line 2043, in __call__
    return self.func(*args)
         ~~~~~~~~~^^^^^^^
File "/Users/zhangxiang/Documents/六六湖秘密探险2.x(最新).py", line 117, in go
    button_b = Button(w, text='鱼C公园', command=go2('鱼C公园'))
                                                 ~~~^^^^^^^^^^^
File "/Users/zhangxiang/Documents/六六湖秘密探险2.x(最新).py", line 49, in go2
    Radiobutton(v, text=power, variable=v, value=i+1, indicatoron=False).pack(anchor=W)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/tkinter/__init__.py", line 3585, in __init__
    Widget.__init__(self, master, 'radiobutton', cnf, kw)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/tkinter/__init__.py", line 2718, in __init__
    self._setup(master, cnf)
    ~~~~~~~~~~~^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/tkinter/__init__.py", line 2687, in _setup
    self.tk = master.tk
            ^^^^^^^^^
AttributeError: 'IntVar' object has no attribute 'tk'. Did you mean: '_tk'?

FishC 发表于 2024-2-9 17:43:15

你好,看起来你在编写一个使用Tkinter实现的游戏界面,但是代码中存在一些错误。以下是一些可能导致错误的地方:

1. 在创建按钮时,你给`command`参数传递了`go2('鱼C公园')`。然而,`command`参数需要的是一个函数名,而不是函数调用的结果。你可以将这个语句改为`command=lambda: go2('鱼C公园')`。

2. `Radiobutton`的第一个参数是父组件,但你传递的是一个`IntVar`对象。你应该将它改为传递`w`,即`Radiobutton(w, text=power, variable=v, value=i+1, indicatoron=False)`。

3. 在connect函数中,position表示的是一个元组,你不能直接进行运算。你需要为元组中的每个元素进行运算。例如,`a_center = ((a + a) / 2, (a + a) / 2)`。

4. 在connect函数的最后,你返回的是四个坐标,但你没有将它们存储在变量中。你需要在函数的结尾添加一个返回语句,例如`return a_x, a_y, b_x, b_y`。

请尝试根据上述建议进行修改,并确保代码的其他部分也正确无误。如果还有其他问题,请随时向我提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

tommyyu 发表于 2024-2-9 18:09:02

换用低版本python(3.9)及所对应的低版本tkinter试试看

hveagle 发表于 2024-2-9 18:12:19

tommyyu 发表于 2024-2-9 18:09
换用低版本python(3.9)及所对应的低版本tkinter试试看

怎么改啊?

tommyyu 发表于 2024-2-9 18:15:44

hveagle 发表于 2024-2-9 18:12
怎么改啊?

下载低版本的 python

tommyyu 发表于 2024-2-9 18:19:10

hveagle 发表于 2024-2-9 18:12
怎么改啊?

刚刚我在我这里试了一下,3.8 运行你这个程序没有问题,应该是 3.13 的 bug 导致出现的问题。

liuhongrun2022 发表于 2024-2-9 18:22:55

修正了你程序的部分问题,具体我懒得介绍了,自己看吧,求一个最佳答案。

from tkinter import *
from math import *
from time import *

food = ['LV4 苹果(48)', 'LV4 苹果(48)', 'LV4 苹果(48)']
power = ['LV3 三jio尺', 'LV3 三jio尺', 'LV3 三jio尺', 'LV3 三jio尺', 'LV3 三jio尺']
full = 500
money = 666

root = Tk()
root.title('六六湖秘密探险 V2.0 Feb.6th 2024 -- 六六游戏城V8888.66.5.1.0.456.17.0a7')

w = Canvas(root, width=550, height=350)
w.pack()

v = IntVar()
v.set(1)

now = '家'

road = {'家':{'鱼C公园':, '食物超市1~6':}, \
      '鱼C公园':{'家':, '食物超市1~6':, '武器超市1~6':}, \
      '食物超市1~6':{'家':, '鱼C公园':, 'LV1打工仔公司':}, \
      '武器超市1~6':{'鱼C公园':, 'LV1打工仔公司':}, \
      'LV1打工仔公司':{'食物超市1~6':, '武器超市1~6':}}

def callback():
    print('啊!我被调用了')

def go2(button):
    if button == now:
      label3 = Label(w, text='你就在这儿')
      label3_window = Canvas.create_window(w, 540, 200, window=label3)
    else:
      w.delete(ALL)
      w.after(500)
      label4 = Label(w, text='Loading... 口口20%')
      label4_window = Canvas.create_window(w, 275, 340, anchor=W, window=label4)
      w.delete(ALL)
      w.after(500)
      label5 = Label(w, text='Loading... 口口口口40%')
      label5_window = Canvas.create_window(w, 275, 340, anchor=W, window=label4)
      w.delete(ALL)
      w.after(500)
      label6 = Label(w, text='【777警报】怪兽来袭!')
      label6_window = Canvas.create_window(w, 275, 340, window=label4)
      lpower = len(power)
      for i in range(lpower):
            Radiobutton(w, text=power, variable=v, value=i+1, indicatoron=False).pack(anchor=W)

position = {'A':((5, 5), (65, 65), 30, 'red', '家'), \
            'B':((70, 70), (130, 130), 30, 'orange', '鱼C公\n园'), \
            'C':((170, 70), (230, 130), 30, 'yellow', '食物超\n市1~6'), \
            'D':((70, 170), (130, 230), 30, 'green', '武器超\n市1~6'), \
            'E':((170, 270), (230, 330), 30, 'blue', 'LV1打\n工仔公\n司')}

def connect(x):
    a = position]
    b = position]
    a_center = ((a + a), (a + a))
    b_center = ((b + b), (b + b))
    if a_center < b_center and a_center < b_center:
      #右下I
      temp = (a_center, b_center)
      s1 = b_center - temp
      s2 = temp - a_center
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center + diff1, a_center + diff2, b_center - diff1, b_center - diff2
    elif a_center > b_center and a_center < b_center:
      #左下II
      temp = (a_center, b_center)
      s1 = temp - b_center
      s2 = temp - a_center
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center - diff1, a_center + diff2, b_center + diff1, b_center - diff2
    elif a_center > b_center and a_center > b_center:
      #左上III
      temp = (a_center, b_center)
      s1 = temp - b_center
      s2 = a_center - temp
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center - diff1, a_center - diff2, b_center + diff1, b_center + diff2
    elif a_center < b_center and a_center > b_center:
      #右上IV
      temp = (a_center, b_center)
      s1 = temp - b_center
      s2 = temp - a_center
      diff1 = 30 * sin(atan(s1/s2))
      diff2 = 30 * sin(atan(s2/s1))
      return a_center + diff1, a_center - diff2, b_center - diff1, b_center + diff2
    elif a_center == b_center:
      #同行
      return a_center + 30, a_center, b_center - 30, b_center
    elif a_center == b_center:
      #同列
      return a_center, a_center + 30, b_center, b_center - 30

def go():
    w.delete(ALL)
    for i in list(position.keys()):
      w.create_oval(position, position, position, position, fill=position)
      center = (position + position, position + position)
      w.create_text(center, center, text=position)

    myline = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E'), ('D', 'E')]

    for each in myline:
      w.create_line(connect(each))

    the_label = Label(w, text='你想去哪儿?')
    the_label_window = Canvas.create_window(w, 540, 10, anchor=E, window=the_label)
    button_a = Button(w, text='家', command=lambda:go2('家'))
    button_a_window = Canvas.create_window(w, 540, 35, anchor=E, window=button_a)
    button_b = Button(w, text='鱼C公园', command=lambda:go2('鱼C公园'))
    button_b_window = Canvas.create_window(w, 540, 60, anchor=E, window=button_b)
    button_c = Button(w, text='食物超市1~6', command=lambda:go2('食物超市1~6'))
    button_c_window = Canvas.create_window(w, 540, 85, anchor=E, window=button_c)
    button_d = Button(w, text='武器超市1~6', command=lambda:go2('武器超市1~6'))
    button_d_window = Canvas.create_window(w, 540, 110, anchor=E, window=button_d)
    button_e = Button(w, text='LV1打工仔公司', command=lambda:go2('LV1打工仔公司'))
    button_e_window = Canvas.create_window(w, 540, 135, anchor=E, window=button_e)

label1 = Label(w, text='六六湖秘密探险 V2.0\nSixty-six Lake Secret Adventure')
label1_window = Canvas.create_window(w, 275, 125, window=label1)
label2 = Label(w, text='你要做什么?')
label2_window = Canvas.create_window(w, 275, 150, window=label2)
choose1 = Button(w, text='出发', command=go)
choose1_window = Canvas.create_window(w, 275, 175, window=choose1)
choose2 = Button(w, text='查看背包', command=callback)
choose1_window = Canvas.create_window(w, 275, 200, window=choose2)

mainloop()

学习编程中的Ben 发表于 2024-2-9 19:03:45

https://geek-docs.com/python/python-ask-answer/82_tk_1701998354.html

降低Python版本即可
页: [1]
查看完整版本: 求助