在 Tkinter 和 Python3 中使用 entry、StringVar 和 IntVar
Using entry, StringVar and IntVar with Tkinter and Python3
我是 python 的新手,在解释内容时请使其对初学者友好 :)。
基本上,当我尝试 运行 使用 tkinter 的 reddit 爬虫时,我得到了各种我不理解的错误,在此先感谢,我真的很感谢社区。
下面是一些基本代码:
def Exas():
import praw
imgcount = 0
reddit = praw.Reddit(client_id='CENSORED',
client_secret='CENSORED',
user_agent='TheBigScraper(By u/scrappermaster)',
username='scrappermaster',
password='thescrapperisscrap')
listoftitles = []
listofurls = []
# whichone = input('what subreddit do you want to access? ')
# endlimit = input('what number of pictures do you want to download? ')
whichoner = whichone
ender = int(endlimit.get())
subreddit = reddit.subreddit(whichone)
for submission in subreddit.hot(limit=int(ender)):
title = submission.title
link = submission.url
Both = title + " " + link
if '.jpg' in link :
listofurls.append(link)
listoftitles.append(title)
再往下大约 50 行:
import tkinter as tk
import colorama
root = tk.Tk()
root.title("InstagramBot")
root.geometry('320x125')
whichone = str(tk.StringVar())
endlimit = tk.StringVar()
lblWhichone = tk.Label(root, text = 'Subreddit Name:').grid(row = 0, column = 0, padx = 0, pady = 10)
entWhichone = tk.Entry(root, textvariable = whichone).grid(row = 0, column = 1)
lblIntendlimit = tk.Label(root, text = 'Number of Pictures:').grid(row = 1, column = 0, padx = 0, pady = 10)
entendlimit = tk.Entry(root, textvariable = endlimit).grid(row = 1, column = 1)
btn = tk.Button(root, text = 'Execute', command = Exas, fg='red', font='Helvetica 18 bold').grid(row = 5, column = 1)
root.mainloop()
root.title("InstagramBot")
奇怪的错误代码:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Users/eucar/Documents/Testing/Compactor2000.py", line 28, in Exas
for submission in subreddit.hot(limit=int(ender)):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/models/listing/generator.py", line 52, in __next__
self._next_batch()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/models/listing/generator.py", line 62, in _next_batch
self._listing = self._reddit.get(self.url, params=self.params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/reddit.py", line 408, in get
data = self.request('GET', path, params=params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/reddit.py", line 534, in request
params=params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prawcore/sessions.py", line 185, in request
params=params, url=url)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prawcore/sessions.py", line 130, in _request_with_retries
raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.Redirect: Redirect to /subreddits/search
我已将 Exas 减少到基于 Tkinter 的最小值。我从未使用过 praw 库,也没有安装它。
def Exas():
whichoner = whichone.get()
ender = int(endlimit.get())
print(whichoner, ender)
为我在评论中提到的 StingVar 修改你的 tkinter window。
import tkinter as tk
root = tk.Tk()
root.title("InstagramBot")
root.geometry('320x125+50+50')
whichone = tk.StringVar()
endlimit = tk.StringVar()
lblWhichone = tk.Label(root, text = 'Subreddit Name:').grid(row = 0, column = 0, padx = 0, pady = 10)
entWhichone = tk.Entry(root, textvariable = whichone).grid(row = 0, column = 1)
# The grid method returns None! All these variables have the value None.
# I don't think it matters for what you're doing but may cause problems for you in the future.
# In general this is better.
# entWhichone = tk.Entry(root, textvariable = whichone)
# entWhichone.grid(row = 0, column = 1)
# entWhichone now points to a tk.Entry object.
lblIntendlimit = tk.Label(root, text = 'Number of Pictures:').grid(row = 1, column = 0, padx = 0, pady = 10)
entendlimit = tk.Entry(root, textvariable = endlimit).grid(row = 1, column = 1)
btn = tk.Button(root, text = 'Execute', command = Exas, fg='red', font='Helvetica 18 bold').grid(row = 5, column = 1)
root.mainloop()
如果我在您的 window 中键入 test 和 42,它会将 Test 和 42 回显到我的控制台。
您最新的错误消息表明 StringVar 已传递给 praw 函数或方法。在 Exas 函数中你需要:
whichoner = whichone.get() # Was it left at ... = whichone ?
如果上述建议没有帮助,请尝试在调用任何其他内容之前打印 whichoner 和 ender 以检查它们 return 您的期望。
一个风格点是在Python中函数和变量通常是小写的,用下划线'_'分隔,lbl_which_one而不是lblWhichone,exas代表Exas。 类 以大写字母命名 tk.Entry 创建一个 class 条目的对象。您的代码仍然有效,只是其他人更难理解。
我是 python 的新手,在解释内容时请使其对初学者友好 :)。 基本上,当我尝试 运行 使用 tkinter 的 reddit 爬虫时,我得到了各种我不理解的错误,在此先感谢,我真的很感谢社区。
下面是一些基本代码:
def Exas():
import praw
imgcount = 0
reddit = praw.Reddit(client_id='CENSORED',
client_secret='CENSORED',
user_agent='TheBigScraper(By u/scrappermaster)',
username='scrappermaster',
password='thescrapperisscrap')
listoftitles = []
listofurls = []
# whichone = input('what subreddit do you want to access? ')
# endlimit = input('what number of pictures do you want to download? ')
whichoner = whichone
ender = int(endlimit.get())
subreddit = reddit.subreddit(whichone)
for submission in subreddit.hot(limit=int(ender)):
title = submission.title
link = submission.url
Both = title + " " + link
if '.jpg' in link :
listofurls.append(link)
listoftitles.append(title)
再往下大约 50 行:
import tkinter as tk
import colorama
root = tk.Tk()
root.title("InstagramBot")
root.geometry('320x125')
whichone = str(tk.StringVar())
endlimit = tk.StringVar()
lblWhichone = tk.Label(root, text = 'Subreddit Name:').grid(row = 0, column = 0, padx = 0, pady = 10)
entWhichone = tk.Entry(root, textvariable = whichone).grid(row = 0, column = 1)
lblIntendlimit = tk.Label(root, text = 'Number of Pictures:').grid(row = 1, column = 0, padx = 0, pady = 10)
entendlimit = tk.Entry(root, textvariable = endlimit).grid(row = 1, column = 1)
btn = tk.Button(root, text = 'Execute', command = Exas, fg='red', font='Helvetica 18 bold').grid(row = 5, column = 1)
root.mainloop()
root.title("InstagramBot")
奇怪的错误代码:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Users/eucar/Documents/Testing/Compactor2000.py", line 28, in Exas
for submission in subreddit.hot(limit=int(ender)):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/models/listing/generator.py", line 52, in __next__
self._next_batch()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/models/listing/generator.py", line 62, in _next_batch
self._listing = self._reddit.get(self.url, params=self.params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/reddit.py", line 408, in get
data = self.request('GET', path, params=params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/reddit.py", line 534, in request
params=params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prawcore/sessions.py", line 185, in request
params=params, url=url)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prawcore/sessions.py", line 130, in _request_with_retries
raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.Redirect: Redirect to /subreddits/search
我已将 Exas 减少到基于 Tkinter 的最小值。我从未使用过 praw 库,也没有安装它。
def Exas():
whichoner = whichone.get()
ender = int(endlimit.get())
print(whichoner, ender)
为我在评论中提到的 StingVar 修改你的 tkinter window。
import tkinter as tk
root = tk.Tk()
root.title("InstagramBot")
root.geometry('320x125+50+50')
whichone = tk.StringVar()
endlimit = tk.StringVar()
lblWhichone = tk.Label(root, text = 'Subreddit Name:').grid(row = 0, column = 0, padx = 0, pady = 10)
entWhichone = tk.Entry(root, textvariable = whichone).grid(row = 0, column = 1)
# The grid method returns None! All these variables have the value None.
# I don't think it matters for what you're doing but may cause problems for you in the future.
# In general this is better.
# entWhichone = tk.Entry(root, textvariable = whichone)
# entWhichone.grid(row = 0, column = 1)
# entWhichone now points to a tk.Entry object.
lblIntendlimit = tk.Label(root, text = 'Number of Pictures:').grid(row = 1, column = 0, padx = 0, pady = 10)
entendlimit = tk.Entry(root, textvariable = endlimit).grid(row = 1, column = 1)
btn = tk.Button(root, text = 'Execute', command = Exas, fg='red', font='Helvetica 18 bold').grid(row = 5, column = 1)
root.mainloop()
如果我在您的 window 中键入 test 和 42,它会将 Test 和 42 回显到我的控制台。 您最新的错误消息表明 StringVar 已传递给 praw 函数或方法。在 Exas 函数中你需要:
whichoner = whichone.get() # Was it left at ... = whichone ?
如果上述建议没有帮助,请尝试在调用任何其他内容之前打印 whichoner 和 ender 以检查它们 return 您的期望。
一个风格点是在Python中函数和变量通常是小写的,用下划线'_'分隔,lbl_which_one而不是lblWhichone,exas代表Exas。 类 以大写字母命名 tk.Entry 创建一个 class 条目的对象。您的代码仍然有效,只是其他人更难理解。