Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python切片中使用lentgh变量=[:]_Python_Tkinter_Combobox - Fatal编程技术网

如何在python切片中使用lentgh变量=[:]

如何在python切片中使用lentgh变量=[:],python,tkinter,combobox,Python,Tkinter,Combobox,如何在[:]中使用lentgh变量 我想使用一个变量,因为我在tkinter中有一个组合框。我得到这个错误 TypeError:切片索引必须是整数或无,或具有索引方法 上面的文章提供了一些关于使用变量动态切片字符串的好技巧 如果我理解您试图正确执行的操作,您将尝试输入长度并根据该长度随机生成密码?将lentgh更改为length您需要使用返回字符串[:lentgh.get()]而不是返回字符串[:lentgh]。正如@mariolu指出的,您需要修复spelling@TheLizzardTNX

如何在
[:]
中使用
lentgh
变量

我想使用一个变量,因为我在
tkinter
中有一个
组合框。我得到这个错误

TypeError:切片索引必须是整数或无,或具有索引方法

上面的文章提供了一些关于使用变量动态切片字符串的好技巧


如果我理解您试图正确执行的操作,您将尝试输入长度并根据该长度随机生成密码?

将lentgh更改为length您需要使用
返回字符串[:lentgh.get()]
而不是
返回字符串[:lentgh]
。正如@mariolu指出的,您需要修复spelling@TheLizzardTNX兄弟worked@Erfan你想让我写一个正确的答案吗?这没有帮助,因为
lentgh
而不是普通的python
int
你试过转换tkinter输入吗?numchoosen=ttk.Combobox(窗口,宽度=12,文本变量=int(lentgh))首先我不是OP第二,在回答包含它的问题之前先看看tkinter.IntVar的工作原理。你对
tkinter.IntVar
的工作方式有错误的想法对不起,伙计,我的坏,只是偶然发现它,然后一拍即合,不假思索地回答,length.get()而不是经典的int()我的坏。
from tkinter import *
from tkinter import ttk
import random

window = Tk()
window.resizable(False , False)
window.title('Password Generator')
window.geometry('400x200')

lentgh = IntVar()
lbl = StringVar()
var1 = StringVar()

Alphabet = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'

showpass = ttk.Label(window , textvariable = lbl).pack()

def randalp():
        string = list(Alphabet)
        random.shuffle(string)
        return string[:lentgh]

ttk.Label(window , text = 'Password Lentgh:').pack()
numchoosen = ttk.Combobox(window, width = 12 , textvariable = lentgh)
numchoosen['values'] = (5,6,7,8,9,10)
numchoosen.pack()
numchoosen.current(2)
numchoosen.config(state = 'readonly')

rad1 = ttk.Checkbutton(window , text = 'Alphabet' , variable = var1).pack()

def btnclick():
        get1 = var1.get()
        if get1 == '1':
                lbl.set(randalp())

btn = ttk.Button(window , text = 'Generate' , command = btnclick).pack()

window.mainloop()