小编Tha*_*ame的帖子

在使用try和except语句赋值之前引用的局部变量

我在try和except语句中遇到一些问题,我有一个条目小部件,它接受字符串输入,但我有代码,后来将它转换为整数,问题是如果用户输入类似文本的东西,它会抛出这样的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
    return self.func(*args)
  File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 157, in buttonclick_gamescreen
    entryx = int(e1.get())
ValueError: invalid literal for int() with base 10: 'abc'
Run Code Online (Sandbox Code Playgroud)

所以我想用try和except语句隐藏错误但我现在得到另一条错误消息.

这就是它在代码中的样子.

    while pressed == 8 :
        try:
            entryx = int(e1.get())
            entryy = int(e2.get())
        except ValueError:
            print("text")

        answerx = answerlistx[randomimage]
        answery = answerlisty[randomimage]

        if entryx == answerx and entryy == answery
            canvas.delete(images)
            randomimage = random.randrange(0,49+1)
            scorecounter = scorecounter + 1
            game = PhotoImage(file=imagelist[randomimage])
            images …
Run Code Online (Sandbox Code Playgroud)

python try-catch

7
推荐指数
2
解决办法
1万
查看次数

使用javascript禁用灯箱

我有一个图像,点击后,将打开一个灯箱(来自http://lokeshdhakar.com/projects/lightbox2/的脚本),我想要做的是禁用当按钮切换为关闭时发生.(因此单击图像不会执行任何操作.)

我尝试使用.off和.unbind在网站上的其他一些答案后禁用灯箱,但它们都没有为我工作.我也跟着如何使用jQuery动态启用/禁用链接?如建议但没有运气.

下面是HTML.

<div style="margin-left:10%;padding:1px 16px;">
  <section id="four_columns">
    <article class="img-item">
      <figure>
        <a href="img/livingroom.jpg" data-lightbox="livingroom"><img id="img_window1" src="img/livingroom.jpg" width="200" height="120"></a>
        <figcaption>
          <strong>Living Room 
            <div class="onoffswitch">
              <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="window1" value="window1" checked>
                <label class="onoffswitch-label" for="window1">
                  <span class="onoffswitch-inner"></span>
                  <span class="onoffswitch-switch"></span>
                </label>
            </div>
          </strong>
        </figcaption>
      </figure>
    </article>
...
Run Code Online (Sandbox Code Playgroud)

和javascript

<script type="text/javascript">
  $(document).ready(function() {
  var full_opacity = 1;
  var faded_opacity = 0.3;
  var fade_speed = 'fast';              
  var objid;

  $('input[name="onoffswitch"]').each(function() {
    objid = "#img_" + $(this).val();

    if($(this).prop('checked')) {
        $(objid).css('opacity', full_opacity);
    }
    else …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery lightbox

6
推荐指数
1
解决办法
1280
查看次数

Python - Tkinter 画布中的文本输入

我想在我用 Tkinter 创建的画布内为整数创建一个用户输入框。我将如何去做这件事?

def gamescreen():
    photo = PhotoImage(file="gamescreen.gif")
    canvas.bind("<Button-1>", buttonclick_gamescreen)
    canvas.pack(expand = YES, fill = BOTH)
    canvas.create_image(1, 1, image = photo, anchor = NW)
    e1 = Entry(canvas)
    e2 = Entry(canvas)
    game1 = PhotoImage(file="1.gif")
    canvas.create_image(30, 65, image = game1, anchor = NW)
    canvas.create_window(window = e1, x=10, y=10)
    canvas.create_window(window = e2 , x=400, y=10)    
    canvas.update()
    window.mainloop()
Run Code Online (Sandbox Code Playgroud)

这是我目前所拥有的,但画布上的任何地方都没有出现输入框。我知道这可能不是在 python 中创建游戏的最有效方式,但我不熟悉任何其他方式。

谢谢你的帮助。

编辑:我已经使用提供的建议更新了代码。我现在有一个问题

IndexError: tuple index out of range
Run Code Online (Sandbox Code Playgroud)

这发生在以下几行

canvas.create_window(window = e1, x=10, y=10)
canvas.create_window(window = e2, x=400, y=10)
Run Code Online (Sandbox Code Playgroud)

编辑:好的,我知道出了什么问题,我不得不删除 x= 和 y= …

python user-input tkinter tkinter-canvas

3
推荐指数
1
解决办法
1万
查看次数

在另一个向量的矢量子集

说我有一个矢量

vector = c('hello','world')
Run Code Online (Sandbox Code Playgroud)

和另外两个矢量

vector2 = c(2,4)
vector3 = c(4,5)
Run Code Online (Sandbox Code Playgroud)

我如何通过其他两个向量创建第四个向量,它是第一个向量中每个元素的子集?就像是

vector[1][vector2[1]:vector3[1]]
Run Code Online (Sandbox Code Playgroud)

所以对于这些载体来说就是这样

vector4 = ('ell','ld')
Run Code Online (Sandbox Code Playgroud)

我试图使用sapply但遇到了障碍,因为我不确定如何编写函数来对它们进行子集化.

vector4 = sapply(vector, function(x) x[vector2:vector3])
Run Code Online (Sandbox Code Playgroud)

r vector subset apply

3
推荐指数
1
解决办法
74
查看次数

Python - if语句内部while循环没有响应

我在while循环中遇到if语句时遇到问题.

while pressed == 8 :
    print(answerlistx[randomimage], answerlisty[randomimage])
    entryx = e1.get()
    entryy = e2.get()
    answerx = answerlistx[randomimage]
    answery = answerlisty[randomimage]
    print(entryx, entryy)
    if e1 == answerx and e2 == answery:
        print("correct")
        canvas.delete(images)
        randomimage = random.randrange(0,49+1)
        scorecounter = scorecounter + 1
        game = PhotoImage(file=imagelist[randomimage])
        images = canvas.create_image(30, 65, image = game, anchor = NW)
        e1.delete(0, END)   
        e2.delete(0, END)
        pressed = ''
    else:
        print("incorrect")
        e1.delete(0, END)   
        e2.delete(0, END)
        pressed = ''
Run Code Online (Sandbox Code Playgroud)

while循环应该检查以查看条目小部件的输入是否与答案匹配,但即使答案正确,它也会转到else语句.我在if语句之前有2个打印语句打印输入,答案只是因为没有它,但它确实正确地显示了它.我还以为它可能与字符串和整数混合在一起所以我将答案列表中的所有答案都改为字符串而没有运气.任何人都能弄清楚它有什么问题吗?提前致谢.

python if-statement while-loop

2
推荐指数
1
解决办法
5138
查看次数